title: Multithreading with condution var and mutex = system err
body: <p>I want to create a class that has a worker (consumer) thread that receives a notification from another thread (producer) via an interface (set_x() function) to execute its instructions.</p>
  <p>For this I want to use a common mutex and a common cond_var.</p>
  <p>Unfortunately the program crashes directly with the error message <em>std::system_error invalid argument</em>.</p>
  <p>In the debugger I could see that this happens on the first access to <em>std::unique_lockstd::mutex mlock(set_x_mtx);</em> in the <em>void foo::output()</em> function.</p>
  <p>However, I can't figure out what the problem is here?
  I have created a very simplified example from my code.</p>
  <p>My Class:</p>
  <pre><code>#include &lt;mutex&gt;
  #include &lt;thread&gt;   
  #include &lt;condition_variable&gt;
  #include &lt;bitset&gt;   
  
  class foo {
  
      public:
          foo();
          int startUp();
          void set_x(const unsigned int bit_no, const bool state);
          
  
      private:
  
          bool x[10];
          bool run;
  
          std::mutex set_x_mtx;
          std::condition_variable set_x_cond;
          std::thread start_output_capture;
          std::thread consumer;
  
          void output();
  };
  
  foo::foo()
  {
      run = false;
  }
  
  void foo::set_x(const unsigned int bit_no, const bool state)
  {
      std::unique_lock&lt;std::mutex&gt; lock(set_x_mtx);
      x[bit_no] = state;
      lock.unlock();
      set_x_cond.notify_one();
  }
  
  void foo::output()
  {
      bool (&amp;ref_x)[10] = x;
      consumer = std::thread([this, &amp;ref_x]
      {
          std::bitset&lt;10&gt; buffer_x = 0;
          std::bitset&lt;10&gt; state_x = 0;
          std::unique_lock&lt;std::mutex&gt; mlock(set_x_mtx);
          set_x_cond.wait(mlock);
          while(run) 
          {
              if(buffer_x.any())
              {
                  for(uint16_t idx = 0; idx &lt; 10; ++idx)           // Navigate through mask bit by bit
                  {
                      if(buffer_x[idx] != state_x[idx])            // Bit status is different
                      {
                          if(buffer_x[idx])                        // should be ON
                          {
                              state_x.flip(idx);                   // Toggle bit in current status
                          }                      
                      } else {
                          state_x.flip(idx);                       // Toggle bit in current status
                      }   
                  }           
              }else {                                             
                  state_x.reset();                                // Clear all bits
              }                                                                                                           
              mlock.unlock();
          }
      });
      consumer.join();
  }
  
  int foo::statUp()
  {
      run = true;
      start_output_capture = std::thread([this]
      {
          output();
      });
  
      return EXIT_SUCCESS;
  }
  </code></pre>
  <p>Main:</p>
  <pre><code>#include &lt;chrono&gt;
  #include &lt;thread&gt;
  #include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  
  #include &quot;foo.hpp&quot;
  
  int main()
  {
      foo test;
      test.startUp();
  
      while(1)
      {
          std::this_thread::sleep_for(std::chrono::milliseconds(250));   
      }
  
      return;
  }
  </code></pre>