title: C++ - display progress bar when using std::async
body: <p>So I'm working on a ray-tracer, and in order to reduce rendering time, I used std::async to do pixel calculations independently. 
  I used <a href="https://medium.com/@phostershop/solving-multithreaded-raytracing-issues-with-c-11-7f018ecd76fa" rel="nofollow noreferrer">this</a> tutorial, and everything works great, and indeed I was able to save about 70% in rendering time.</p>
  
  <p>Still, some scenes take a while to render, and I'd like to display some-sort of progress bar. As I'm fairly a newbie to async infra, I'm not quite sure how to do that. I'd like some sort of mechanism to only print the progress from the 'main', calling', thread.</p>
  
  <p>Here the rendering loop - notice the commented lines for progress bar - which obviously should not go there:</p>
  
  <pre><code>Image *image = new Image(width, height);
  
  size_t max = width * height;
  size_t cores = std::thread::hardware_concurrency();
  volatile atomic&lt;size_t&gt; count(0);
  vector&lt;future&lt;void&gt;&gt; future_vector;
  
  while (cores--)
  {
      future_vector.push_back(
          std::async(launch::async, [=, &amp;camera, &amp;scene, &amp;count]()
          {
              while (true)
              {
                  std::size_t index = count++;
                  if (index &gt;= max)
                      break;
  
                  GLuint i = index % width;
                  GLuint j = index / width;
  
                  Ray ray = camera.generateRay(i + .5, j - .5);
                  vec3 color = recursiveRayTrace(scene, ray, maxDepth);
                  image-&gt;setPixel(i, j, color);
  
                  // THIS IS BAD
                  //std::cout &lt;&lt; "Progress: [ "&lt;&lt; setprecision(1) &lt;&lt; fixed &lt;&lt; (count / (GLfloat)max) * 100.0 &lt;&lt; "% ] \r";
                  //std::cout.flush();
  
              }
          }));
  }
  
  for (auto&amp; e : future_vector) {
      e.get();
  }
  
  return image;
  </code></pre>
  
  <p><strong>UPDATE:</strong>
  So I did what some answers suggested - here are the results - for future readers. 
  My benchmark for a few scenes was 15 seconds (I took something fast on purpose), when rendering asynchronously.
  When I used mutex, time was slowed down to 26 seconds. (Still better than 1.32 on single-thread). 
  I also actively waited for one of the working threads, and kept updating the progress every 100mili or so - and render time was 16 seconds. So I'm very pleased with this result, as the printing barely slowed the process. </p>
  
  <p>Thanks,
  Alon</p>