title: multithreaded program producer/consumer [boost]
body: <p>I'm playing with boost library and C++. I want to create a multithreaded program that contains a producer, conumer, and a stack. The procuder fills the stack, the consumer remove items (int) from the stack. everything work (pop, push, mutex) But when i call the pop/push winthin a thread, i don't get any effect </p>
  
  <p>i made this simple code  : </p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;stack&gt;
  #include &lt;iostream&gt;
  #include &lt;algorithm&gt;
  #include &lt;boost/shared_ptr.hpp&gt;
  #include &lt;boost/thread.hpp&gt;
  #include &lt;boost/date_time.hpp&gt; 
  #include &lt;boost/signals2/mutex.hpp&gt;
  #include &lt;ctime&gt;
  
  using namespace std;
  
  / *
  * this class reprents a stack which is proteced by mutex
  * Pop and push are executed by one thread each time.
  */
  class ProtectedStack{
  private : 
  stack&lt;int&gt; m_Stack;
  boost::signals2::mutex m;
  
  public : 
  ProtectedStack(){
  }
  ProtectedStack(const ProtectedStack &amp; p){
  
  }
  void push(int x){
      m.lock();
      m_Stack.push(x);
      m.unlock();
  }
  
  void pop(){
      m.lock();
      //return m_Stack.top();
      if(!m_Stack.empty())
          m_Stack.pop();
      m.unlock(); 
  }
  int size(){
      return m_Stack.size();
  }
  bool isEmpty(){
      return m_Stack.empty();
  }
  int top(){
      return m_Stack.top();
  }
  };
  
  /*
  *The producer is the class that fills the stack. It encapsulate the thread object 
  */
  
  class Producer{
  public:
  Producer(int number ){
      //create thread here but don't start here
  m_Number=number;
  
  
  }
  void fillStack (ProtectedStack&amp; s ) {
      int object = 3; //random value
      s.push(object);
      //cout&lt;&lt;"push object\n";
  }
  
  void produce (ProtectedStack &amp; s){
      //call fill within a thread 
      m_Thread = boost::thread(&amp;Producer::fillStack,this, s);  
  }
  
   private :
  int m_Number;
  boost::thread m_Thread;
  
  };
  
  
  /* The consumer will consume the products produced by the producer */ 
  
  class Consumer {
  private : 
  int m_Number;
  boost::thread m_Thread;
  public:
  Consumer(int n){
      m_Number = n;
  }
  
  void remove(ProtectedStack &amp;s ) {
  
       if(s.isEmpty()){ // if the stack is empty sleep and wait for the producer      to fill the stack
          //cout&lt;&lt;"stack is empty\n";
          boost::posix_time::seconds workTime(1); 
          boost::this_thread::sleep(workTime);
       }
       else{
          s.pop(); //pop it
          //cout&lt;&lt;"pop object\n";
  
       }
  
  }
  
  void consume (ProtectedStack &amp; s){
      //call remove within a thread 
      m_Thread = boost::thread(&amp;Consumer::remove, this, s);  
  }
  
  };
  
  
  int main(int argc, char* argv[])  
  {  
  
  
  
  ProtectedStack s;
  
  
      Producer p(0);
      p.produce(s);
  
      Producer p2(1);
      p2.produce(s);
  
      cout&lt;&lt;"size after production "&lt;&lt;s.size()&lt;&lt;endl;
      Consumer c(0);
      c.consume(s);
      Consumer c2(1);
      c2.consume(s);
      cout&lt;&lt;"size after consumption  "&lt;&lt;s.size()&lt;&lt;endl;
  
  getchar();
  return 0;  
  }  
  </code></pre>
  
  <p>After i run that in VC++ 2010 / win7 
  i got : 
  0
  0</p>
  
  <p>Could you please help me understand why when i call fillStack function from the main i got an effect but when i call it from a thread nothing happens? 
  Thank you</p>