title: Boost mutex locking on same thread
body: <p>I'm new to the boost library, and it's such an amazing library! Also, I am new to mutexes, so forgive me if I am making a newbie mistake.</p>
  
  <p>Anyway, I have two functions called <code>FunctionOne</code> and <code>FunctionTwo</code>. <code>FunctionOne</code> and <code>FunctionTwo</code> are called asynchronously by a different thread. So here's what happens: In <code>FunctionOne</code>, I lock a global mutex at the beginning of the function and unlock the global mutex at the end of the function. Same thing for <code>FunctionTwo</code>.</p>
  
  <p>Now here's the problem: at times, <code>FunctionOne</code> and <code>FunctionTwo</code> are called less than a few milliseconds apart (not always though). So, <code>FunctionOne</code> begins to execute and half-way through <code>FunctionTwo</code> executes. When <code>FunctionTwo</code>  locks the mutex, the entire thread that <code>FunctionOne</code> and <code>FunctionTwo</code> are on is stopped, so <code>FunctionOne</code> is stuck half-way through and the thread waits on itself in <code>FunctionTwo</code> forever. So, to summarize:</p>
  
  <ul>
  <li>Function 1 locks mutex and begins executing code.</li>
  <li>Function 2 is called a few ms later and locks the mutex, freezing the thread func 1 and 2 are on.</li>
  <li>Now func 1 is stuck half-way through and the thread is frozen, so func 1 never finishes and the mutex is locked forever, waiting for func 1 to finish.</li>
  </ul>
  
  <p>What does one do in such situations? Here is my code:</p>
  
  <pre><code>boost::mutex g_Mutex;
  lua_State* L;
  
  // Function 1 is called from some other thread
  void FunctionOne()
  {
      g_Mutex.lock();
  
      lua_performcalc(L);
  
      g_Mutex.unlock();
  }
  
  // Function 2 is called from some other thread a few ms later, freezing the thread
  // and Function 1 never finishes
  void FunctionTwo()
  {
      g_Mutex.lock();
  
      lua_performothercalc(L);
  
      g_Mutex.unlock();
  }
  </code></pre>