title: How to make a async call function on C++ using threads called from Swift code?
body: <p>I'm trying make a simple async call using C++.  My problem is that my code executing sync and use block functions. I want a async and non-blocking program.</p>
  
  <p>First time, I wrote a little code in C++ to test my logic, so the program ends before thread so this not works. But on the Xcode on iOS project I can write C++ code without <code>main</code> function, on the Final Objective part is detailed it.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;chrono&gt;
  #include &lt;future&gt;
  
  using namespace std;
  
  void call_from_async()
  {
      std::this_thread::sleep_for(std::chrono::seconds(5));
      cout &lt;&lt; "Async call" &lt;&lt; endl;
  }
  
  int main(void) {
      printf("1\n");
      std::future&lt;void&gt; fut = std::async(std::launch::async, call_from_async);
      // std::async(std::launch::async,call_from_async);
      printf("2\n");
      return 0;
  }
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>1
  2
  </code></pre>
  
  <p><strong>Desired output:</strong></p>
  
  <pre><code>1
  2
  Async Call
  </code></pre>
  
  <p><strong>Final Objective</strong></p>
  
  <p>I've a Swift project and I must call C++ async functions. To do it, I want call a C++ function from Swift and pass a pointer function to get the callback when process has been finished. To start the project I just wrote a little code to call C++ function and leave the work make on background, when work is finished back to Swift throught callback function. I don't make a callback function yet, before I want test the async functions.</p>
  
  <p>Swift code</p>
  
  <pre><code>// ViewController.swift
  
  import UIKit
  
  class ViewController: UIViewController {
  
      override func viewDidLoad() {
          super.viewDidLoad()
          printFromCPP()
          print("1")
          call_async_function()
          print("2")
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>// Ex_iOS_CPP_Swift-Bridging-Header.h
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  #include "Foo.h"
  
  #ifdef __cplusplus
  }
  #endif
  </code></pre>
  
  <hr>
  
  <pre><code>// Foo.h
  
  #ifndef Foo_h
  #define Foo_h
  
  void printFromCPP();
  void call_async_function();
  
  #endif /* Foo_h */
  </code></pre>
  
  <hr>
  
  <pre><code>// Foo.cpp
  
  #include &lt;iostream&gt;
  #include &lt;future&gt;
  #include &lt;unistd.h&gt;
  #include "Ex_iOS_CPP_Swift-Bridging-Header.h"
  
  using namespace std;
  
  void called_from_async() {
      sleep(3);
      cout &lt;&lt; "Async call" &lt;&lt; endl;
  }
  
  void call_async_function() {
  //    std::future&lt;void&gt; fut = std::async(std::launch::async, called_from_async);
      std::future&lt;void&gt; result( std::async(called_from_async));
  }
  
  void printFromCPP() {
      cout &lt;&lt; "Hello World from CPP" &lt;&lt; endl;
  }
  </code></pre>
  
  <p><strong>Output</strong></p>
  
  <pre><code>Hello World from CPP
  1
  Async call
  2
  </code></pre>
  
  <p><strong>Desired output</strong></p>
  
  <pre><code>Hello World from CPP
  1
  2
  Async call
  </code></pre>