title: Program abort hangs the named mutex
body: <p>I have several processes but only one should be running at the time. This means that let's say the <strong>Process1</strong> is running and if the <strong>Process2</strong> get launched, then <strong>Process2</strong> should wait until <strong>Process1</strong> is complete. I am considering the <strong>boost named_mutex</strong> for this purpose. In order to avoid a scenario where mutex may not get released if some exception is thrown, it looks like <strong>boost::lock_guard</strong> could be useful. I came up with the following simplified version of the code.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;boost/interprocess/sync/named_mutex.hpp&gt;
  #include &lt;boost/thread.hpp&gt;
  #include &lt;chrono&gt;
  #include &lt;thread&gt;
  
  using namespace boost::interprocess;
  #pragma warning(disable: 4996)
  int main()
  {
  
  
      std::cout &lt;&lt; "Before taking lock" &lt;&lt; std::endl;
  
      named_mutex mutex(open_or_create, "some_name");
      boost::lock_guard&lt;named_mutex&gt; guard(mutex) ;
  
      // Some work that is simulated by sleep
      std::cout &lt;&lt; "now wait for 10 second" &lt;&lt; std::endl;
      std::this_thread::sleep_for(std::chrono::seconds(10));
  
      std::cout &lt;&lt; "Hello World";
  
  
  }
  </code></pre>
  
  <p>So far, so good. When this program is running, I hit Ctl+C so the program gets aborted (kind of simulation of program crashed, unhandled exception etc). After that when I run the application, the program gets hung on the following line of code.</p>
  
  <pre><code>named_mutex mutex(open_or_create, "some_name");
  boost::lock_guard&lt;named_mutex&gt; guard(mutex) ;
  </code></pre>
  
  <p>If I change the mutex name, then it works fine without getting hung. However, it looks like mutex named <strong>some_name</strong> is somehow "remembered" on the machine in some sort of bad state. This results in any application that tries to acquire a mutex with name <strong>some_name</strong> gets hung on this line of code. If I change this mutex name to let' say <strong>some_name2</strong>, the program works fine again.</p>
  
  <ol>
  <li>Can someone please explain what is causing this behavior?</li>
  <li>How can I reset the behavior for this particular mutex?  </li>
  <li>Most importantly, how to avoid this scenario in a real application?</li>
  </ol>