title: Producer/Consumer Implementation -- Feedback Wanted
body: <p>I'm preparing for an interview in a few weeks and I thougth I would give threads in boost a go, as well as do the simple producer/consumer problem I learned in school.</p>
  
  <p>Haven't done it quite awhile so I was curious what you guys think of this? What should I add to make it a better example etc. Thanks for the feedback! :)</p>
  
  <pre><code>//////////////////////////////////////////////////////////////////////////
  boost::mutex bufferMutex;
  deque&lt;int&gt; buffer;
  const int maxBufferSize = 5;
  //////////////////////////////////////////////////////////////////////////
  
  bool AddToBuffer(int i)
  {
      if (buffer.size() &lt; maxBufferSize)
      {
          buffer.push_back(i);
          return true;
      }
      else
      {       
          return false;
      }
  }
  
  bool GetFromBuffer(int&amp; toReturn)
  {
      if (buffer.size() == 0)
      {
          return false;
      }
      else
      {
          toReturn = buffer[buffer.size()-1];
          buffer.pop_back();
          return true;
      }
  }
  
  struct Producer 
  {
      int ID;
      void operator()()
      {
          while (true)
          {
              boost::mutex::scoped_lock lock(bufferMutex);
              int num = dice();
              bool result = AddToBuffer(num);
              lock.unlock();
              //safe area done
              if (result)
              {
                  cout &lt;&lt; "Producer " &lt;&lt; this-&gt;ID &lt;&lt; " Added " &lt;&lt; num &lt;&lt; endl;
              }
              else
              {
                  cout &lt;&lt; "!!Buffer was Full!!" &lt;&lt; endl;
              }
              //Added
              //Now wait
              boost::xtime xt;
              xtime_get( &amp;xt, boost::TIME_UTC);
              xt.nsec += 1000000 + 100000 * (rand() % 1000);
              boost::thread::sleep(xt);
          }
      }
  };
  
  struct Consumer 
  {
      int ID;
      void operator()()
      {
          while (true)
          {
              int returnedInt = 0;
              boost::mutex::scoped_lock lock(bufferMutex);
              bool result = GetFromBuffer(returnedInt);
              lock.unlock();
              //safe area done
              if (result)
              {
                  cout &lt;&lt; "\tConsumer " &lt;&lt; this-&gt;ID &lt;&lt; " Took Out " &lt;&lt; returnedInt &lt;&lt; endl;
              }
              else
              {
                  cout &lt;&lt; "!!Buffer was Empty!!" &lt;&lt; endl;
              }
              //Added
              //Now wait
              boost::xtime xt;
              xtime_get( &amp;xt, boost::TIME_UTC);
              xt.nsec += 1000000 + 100000 * (rand() % 1000);
              boost::thread::sleep(xt);
          }
      }
  };
  
  
  
  void main()
  {
      Producer p, p2;
      Consumer c, c2;
  
      p.ID = 1;
      p2.ID = 2;
  
      c.ID = 1;
      c2.ID = 2;
  
      boost::thread thread1(boost::ref(p));
      boost::thread thread2(boost::ref(c));
      boost::thread thread3(boost::ref(p2));
      boost::thread thread4(boost::ref(c2));
  
      int x;
      cin &gt;&gt; x;
  }
  </code></pre>