title: C++: Is a mutex with `std::lock_guard` enough to synchronize two `std::thread`s?
body: <p>My question is based on below sample of C++ code</p>
  
  <pre><code>#include &lt;chrono&gt;
  #include &lt;thread&gt;
  #include &lt;mutex&gt;
  #include &lt;iostream&gt;
  
  class ClassUtility
  {
  public:
      ClassUtility() {}
      ~ClassUtility() {}
  
      void do_something() {
        std::cout &lt;&lt; "do something called" &lt;&lt; std::endl;
  
        using namespace std::chrono_literals;
        std::this_thread::sleep_for(1s);
      }
  };
  
  
  int main (int argc, const char* argv[]) {
  
    ClassUtility g_common_object;
  
    std::mutex  g_mutex;
  
    std::thread worker_thread_1([&amp;](){
  
        std::cout &lt;&lt; "worker_thread_1 started" &lt;&lt; std::endl;
  
        for (;;) {
  
            std::lock_guard&lt;std::mutex&gt; lock(g_mutex);
  
            std::cout &lt;&lt; "worker_thread_1 looping" &lt;&lt; std::endl;
            g_common_object.do_something();
        }
    });
  
  
    std::thread worker_thread_2([&amp;](){
  
        std::cout &lt;&lt; "worker_thread_2 started" &lt;&lt; std::endl;
  
        for (;;) {
  
            std::lock_guard&lt;std::mutex&gt; lock(g_mutex);
  
            std::cout &lt;&lt; "worker_thread_2 looping" &lt;&lt; std::endl;
            g_common_object.do_something();
        }
    });
  
  
    worker_thread_1.join();
    worker_thread_2.join();
  
    return 0;
  }
  </code></pre>
  
  <p>This is more of a question to get my understanding clear rather &amp; get a sample usage of <code>std::condition_variable</code> iff required.</p>
  
  <p>I have 2 C++ <code>std::thread</code>s which start up in <code>main</code> method. Its a console app on <code>osx</code>. So compiling it using clang. Both the threads use a common object of
  <code>ClassUtility</code> to call a method do some heavy task. For this sample code to explain the situation, both the threads run an infinite loop &amp; close down only when 
  the app closes i.e. when I press <code>ctrl+c</code> on the console. </p>
  
  <p><strong>Seek to know:</strong></p>
  
  <p>Is it correct if I jus use a <code>std::lock_guard</code> on <code>std::mutex</code> to synchronize or protect the calls made to the <code>common_obejct</code> of <code>ClassUtility</code>. Somehow, I seem
  to be getting into trouble with this "<em>just a mutex approach</em>". None of the threads start if I lock gaurd the loops using mutex. Moreover, I get segfaults sometimes. Is this because they are lambdas ?
  assigned to each thread ?</p>
  
  <p>Is it better to use a <code>std::condition_variable</code> between the 2 threads or lambdas to signal &amp; synchronize them ? If yes, then how would the <code>std::condition_variable</code> be used
  here between the lambdas ?</p>
  
  <p><strong>Note:</strong> As the question is only to seek information, hence the code provided here might not compile. It is just to provide a real scenario</p>