title: How std::mutex got unlocked in different thread?
body: <p>I was reading differences between binary semaphore and mutex (<a href="https://stackoverflow.com/questions/62814/difference-between-binary-semaphore-and-mutex">Difference between binary semaphore and mutex</a>) and one thing that i want to verify is that when a task locks (acquires) a mutex only it can unlock (release) it. If another task tries to unlock a mutex it hasn’t locked (thus doesn’t own) then an error condition is encountered and, most importantly, the mutex is not unlocked and for that i created below code in c++14:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;thread&gt;
  #include &lt;mutex&gt;
  #include &lt;chrono&gt;
  using namespace std;
  
  int counter;
  int i;
  std::mutex g_pages_mutex;
  void increment()
  {
      std::cout&lt;&lt;"increment...."&lt;&lt;std::endl;    
      g_pages_mutex.lock();
      bool flag = g_pages_mutex.try_lock();
      std::cout&lt;&lt;"increment Return value is "&lt;&lt;flag&lt;&lt;std::endl;
      counter++;
      std::this_thread::sleep_for(5s);
  }
  void increment1()
  {
      std::this_thread::sleep_for(5s);    
      std::cout&lt;&lt;"increment1...."&lt;&lt;std::endl;       
      g_pages_mutex.unlock();    
      counter++;
      bool flag = g_pages_mutex.try_lock();
      std::cout&lt;&lt;"increment1 Return value is "&lt;&lt;flag&lt;&lt;std::endl;
  }
  int main()
  {
      counter = 0;
      std::thread t(increment);
      std::thread t1(increment1);
      t.join();
      t1.join();
      return 0;
  }
  </code></pre>
  
  <p>However with this example I was able to unlock mutex from thread that doesn't own that , so just want is there some understanding gap or is this issue in c++14 std::mutex ?</p>