title: Using std::mutex as member variable in a class
body: <p>I have defined a class that has <code>std::mutex my_mutex</code> as its private member variable. But when I try to use it using <code>lock_guard</code> in a member function which is called from different threads, the compiler throws up a lots of errors. If I keep this mutex outside the class it works. The code is as follows</p>
  
  <pre><code>class ThreadClass
  {
    std::mutex my_mutex;
    public: 
       void addToList(int max, int interval)
      {
  
        std::lock_guard&lt;std::mutex&gt; guard(my_mutex);
        for (int i = 0; i &lt; max; i++) 
         {
              // Some operation
         }
      }
  };
  
  
   int main()
   {
      std::thread ct(&amp;ThreadClass::addToList,ThreadClass(),100,1);
      std::thread ct2(&amp;ThreadClass::addToList,ThreadClass(),100,10);
      std::thread ct3(&amp;ThreadClass::print,ThreadClass());
  
       ct.join();
       ct2.join();
       ct3.join();
    }
  </code></pre>
  
  <p>If the same <code>my_mutex</code> is  kept out of the class then it works fine. So when the same variable is within the class and called within the member function acted on by a thread, does it treat like a static member? </p>