Python-like multiprocessing in C++
<p>I am new to C++, and I am coming from a long background of Python. </p>

<p>I am searching for a way to run a function in parallel in C++. I read a lot about <code>std::async</code>, but it is still not very clear for me. </p>

<ol>
<li><p>The following code does some really interesting thing</p>

<pre><code>#include &lt;future&gt;
#include &lt;iostream&gt;

void called_from_async() {
  std::cout &lt;&lt; "Async call" &lt;&lt; std::endl;
}

int main() {
  //called_from_async launched in a separate thread if possible
  std::future&lt;void&gt; result( std::async(called_from_async));

  std::cout &lt;&lt; "Message from main." &lt;&lt; std::endl;

  //ensure that called_from_async is launched synchronously
  //if it wasn't already launched
  result.get();

  return 0;
}
</code></pre>

<p>If I run it several times sometimes the output is what I expected:</p>

<pre><code>Message from main.
Async call
</code></pre>

<p>But sometimes I get something like this:</p>

<pre><code>MAessysnacg ec aflrlom main.
</code></pre>

<p>Why isnt the <code>cout</code> happens first? I clearly call the <code>.get()</code> method AFTER the <code>cout</code>. </p></li>
<li><p>About the parallel runs. In case I have a code like this:</p>

<pre><code>#include &lt;future&gt;
#include &lt;iostream&gt;
#include &lt;vector&gt;

int twice(int m) {
  return 2 * m;
}

int main() {
  std::vector&lt;std::future&lt;int&gt;&gt; futures;

  for(int i = 0; i &lt; 10; ++i) {
    futures.push_back (std::async(twice, i));
  }

  //retrive and print the value stored in the future
  for(auto &amp;e : futures) {
    std::cout &lt;&lt; e.get() &lt;&lt; std::endl;
  }

  return 0;
}
</code></pre>

<p>All the 10 calls to <code>twice</code> function will run on separate cores simultaneously? </p>

<p>In case not, is there a similar thing in C++ like the Python <em>multiprocess</em> lib? </p>

<p>Mainly what I am searching for:</p>

<p>I write a function, and call it with n number of inputs with ?multiprocessing? and it will run the function 1 times on n nodes at the same time.</p></li>
</ol>

<p>1) <code>result.get();</code> does not start the thread. It only <strong>waits</strong> for the result. The parallel thread is launched with <code>std::async(called_from_async)</code> call (or whenever the compiler decides).</p>

<p>However <code>std::cout</code> is guaranteed to be internally thread safe. So the result you are showing us <strong>should not</strong> ever happen. There's a race condition, but you can't mix both outputs like that. If it really happens (which I doubt) then you might be dealing with a compiler bug.</p>

<p>2) Your calls will run parallely. On how many cores it depends on OS and other processes running on your machine. But there's a good chance that all will be used (assuming that you have control over whole ecosystem and no other cpu-intensive processes are running in the background).</p>

<p>There is no multiprocessing-like lib for C++ (at least not in the std). If you wish to run subprocesses then there are several options, e.g. forking or <code>popen</code> syscalls.</p>

python c++ asynchronous multiprocessing