title: Boost::Mutex in class not thread-safe
body: <p>I'm learning concurrent programming and what I want to do is have a class where each object it responsible for running its own Boost:Thread. I'm a little over my head with this code because it uses A LOT of functionality that I'm not that comfortable with (dynamically allocated memory, function pointers, concurrency etc etc). It's like every line of code I had to check some references to get it right.</p>
  
  <p>(Yes, all allocated memory is accounted for in the real code!)</p>
  
  <p>I'm having trouble with the mutexes. I declare it static and it seems to get the same value for all the instances (as it should). The code is STILL not thread safe. </p>
  
  <p>The mutex should stop the the threads (right?) from progressing any further in case someone else locked it. Because mutexes are scoped (kind of a neat functionality) and it's within the if statement that should look the other threads out no? Still I get console out puts that clearly suggests it is not thread safe.</p>
  
  <p>Also I'm not sure I'm using the static vaiable right. I tried different ways of refering to it (Seller::ticketSaleMutex) but the only thing that worked was "this->ticketSaleMutex" which seems very shady and it seems to defeat the purpose of it being static.</p>
  
  <p>Seller.h:</p>
  
  <pre><code>class Seller
  {
  public:     
      //Some vaiables
  private:
      //Other variables
      static boost::mutex ticketSaleMutex;      //Mutex definition
  };
  </code></pre>
  
  <p>Seller.cpp:</p>
  
  <pre><code>boost::mutex Seller::ticketSaleMutex;         //Mutex declaration
  
  void Seller::StartTicketSale()
  {
      ticketSale = new boost::thread(boost::bind(&amp;Seller::SellTickets, this));
  
  }
  void Seller::SellTickets()
  {
      while (*totalSoldTickets &lt; totalNumTickets)
      {
          if ([Some time tick])
          {
              boost::mutex::scoped_lock(this-&gt;ticketSaleMutex);
              (*totalSoldTickets)++;
              std::cout &lt;&lt; "Seller " &lt;&lt; ID &lt;&lt; " sold ticket " &lt;&lt; *totalSoldTickets &lt;&lt; std::endl;
          }
  
      }
  }
  </code></pre>
  
  <p>main.cpp:</p>
  
  <pre><code>int main(int argc, char**argv)
  {
      std::vector&lt;Seller*&gt; seller;
      const int numSellers = 10;
      int numTickets = 40;
      int *soldTickets = new int;
      *soldTickets = 0;
      for (int i = 0; i &lt; numSellers; i++)
      {
          seller.push_back(new Seller(i, numTickets, soldTickets));
          seller[i]-&gt;StartTicketSale();
      }
  }
  </code></pre>