title: Python-like multiprocessing in C++
body: <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>