title: Program output appears only sometimes in multi-threaded program
body: <p>I'm using boost threads to parallelize the calculations in my program. A controller object manages the calculation jobs and the results. I create a bunch of worker threads which get their jobs from the controller object while the main thread displays the results. The results need to be shown in the correct order. To achieve this I use boost futures in a <code>std::deque</code>. <code>GetNewJob()</code> adds a new <code>boost::promise</code> to the end of the deque and returns a pointer. <code>GetNextResult()</code> takes a result from the front end of the queue. If no result is ready yet, it blocks the calling thread.</p>
  
  <p>The important parts of my Controller class:</p>
  
  <pre><code>class Controller
  {
  public:
      Controller();
      boost::shared_ptr&lt;boost::promise&lt;Results&gt; &gt; GetNewJob();
      Results GetNextResult();
  
      class NoJobsLeft{};
      class NoResultsLeft{};
  
  private:
      bool JobsLeft() const;
      bool ResultsLeft() const;
  
      std::deque&lt;boost::shared_ptr&lt;boost::promise&lt;Results&gt; &gt; &gt; queue_;
      boost::mutex mutex_;
      boost::condition_variable condition_;
  };
  </code></pre>
  
  <p>Worker function:</p>
  
  <pre><code>void DoWork()
  {
      try
      {
          while(true)
          {
              boost::shared_ptr&lt;boost::promise&lt;Results&gt; &gt;
                      promise(controller.GetNewJob());
  
              //do calculations
  
              promise-&gt;set_value(results);
          }
      }
      catch(NoJobsLeft)
      {
      }
  }
  </code></pre>
  
  <p>Main program code:</p>
  
  <pre><code>Controller controller(args);
  
  boost::thread_group worker_threads;
  
  for (unsigned i = 0; i &lt; n_cpus; ++i)
      worker_threads.create_thread(DoWork);
  
  try
  {
      while(true)
      {
          Results results = controller.GetNextResult();
  
          std::cout &lt;&lt; results;
          std::cout &lt;&lt; std::endl;
      }
  }
  catch(NoResultsLeft)
  {
  }
  
  worker_threads.join_all();
  </code></pre>
  
  <p>Sometimes this works just fine, all results are displayed. <strong>But very often I cannot see any output at all.</strong></p>
  
  <p>I do not use <code>cout</code> in the worker threads.</p>
  
  <hr>
  
  <p>Implementations of <code>GetNewJob()</code>, <code>GetNextResult()</code>:</p>
  
  <pre><code>boost::shared_ptr&lt;boost::promise&lt;Results&gt; &gt; Controller::GetNewJob()
  {
      boost::lock_guard&lt;boost::mutex&gt; lock(mutex_);
  
      if (!JobsLeft())
          throw NoJobsLeft();
  
      //determine more information about the job, not important here
  
      queue_.push_back(boost::make_shared&lt;boost::promise&lt;Results&gt; &gt;());
  
      condition_.notify_one();
  
      return queue_.back();
  }
  
  
  Results Controller::GetNextResult()
  {
      boost::shared_ptr&lt;boost::promise&lt;Results&gt; &gt; results;
      {
          boost::unique_lock&lt;boost::mutex&gt; lock(mutex_);
  
          if (!ResultsLeft())
              throw NoResultsLeft();
  
          while(!queue_.size())
          {
              condition_.wait(lock);
          }
  
          results = queue_.front();
          queue_.pop_front();
      }
  
      return results-&gt;get_future().get();
  }
  
  bool Controller::ResultsLeft() const
  {
      return (queue_.size() || JobsLeft()) ? true : false;
  }
  </code></pre>