title: Mutex can't acquire lock
body: <p>I have a problem where one of my functions can't aquire the lock on one of the 2 mutexes I use.
  I did a basic debug in VC++2010 , setting some breakpoints and it seems if anywhere the lock is acquired, it does get unlocked.</p>
  
  <p>The code that uses mutexes is as follow:</p>
  
  <pre><code>#define SLEEP(x) { Sleep(x); }
  #include&lt;windows.h&gt;
  
      void Thread::BackgroundCalculator( void *unused ){
      while( true ){
          if(MUTEX_LOCK(&amp;mutex_q, 5) == 1){
              if(!QueueVector.empty()){
  //cut
                  MUTEX_UNLOCK(&amp;mutex_q);
                      //cut
                  while(MUTEX_LOCK(&amp;mutex_p,90000) != 1){}
                  //cut
                  MUTEX_UNLOCK(&amp;mutex_p);
              }
  
          }
          SLEEP(25);
      }
  }
  </code></pre>
  
  <p>Then somwhere else:</p>
  
  <pre><code>PLUGIN_EXPORT void PLUGIN_CALL
      ProcessTick(){
      if(g_Ticked == g_TickMax){
          if(MUTEX_LOCK(&amp;mutex_p, 1) == 1){
              if(!PassVector.empty()){
                  PassVector.pop();
              }
              MUTEX_UNLOCK(&amp;mutex_p);
          }
          g_Ticked = -1;
      }
      g_Ticked += 1;
  }
  </code></pre>
  
  <hr>
  
  <pre><code>static cell AMX_NATIVE_CALL n_CalculatePath( AMX* amx, cell* params ){
      if(MUTEX_LOCK(&amp;mutex_q,1) == 1){
          QueueVector.push_back(QuedData(params[1],params[2],params[3],amx));
          MUTEX_UNLOCK(&amp;mutex_q);
          return 1;
      }
      return 0;
  }
  </code></pre>
  
  <p>init:</p>
  
  <pre><code>PLUGIN_EXPORT bool PLUGIN_CALL Load( void **ppData ) {
      MUTEX_INIT(&amp;mutex_q);
      MUTEX_INIT(&amp;mutex_p);
      START_THREAD( Thread::BackgroundCalculator, 0);
      return true;
  }
  </code></pre>
  
  <p>Some variables and functions:</p>
  
  <pre><code>int MUTEX_INIT(MUTEX *mutex){
      *mutex = CreateMutex(0, FALSE, 0);
      return (*mutex==0);
  }
  
  int MUTEX_LOCK(MUTEX *mutex, int Timex = -1){
      if(WaitForSingleObject(*mutex, Timex) == WAIT_OBJECT_0){
          return 1;
      }
      return 0;
  }
  int MUTEX_UNLOCK(MUTEX *mutex){
      return ReleaseMutex(*mutex);
  }
  
  MUTEX mutex_q = NULL;
  MUTEX mutex_p = NULL;
  </code></pre>
  
  <p>and defines:</p>
  
  <pre><code>#   include &lt;process.h&gt;
  #   define OS_WINDOWS
  #   define MUTEX HANDLE
  #   include &lt;Windows.h&gt;
  #   define EXIT_THREAD() { _endthread(); }
  #   define START_THREAD(a, b) { _beginthread( a, 0, (void *)( b ) ); }
  </code></pre>
  
  <p>Thread header file:</p>
  
  <pre><code>#ifndef __THREAD_H
  #define __THREAD_H
  
  class Thread{
      public:
                                      Thread                      ( void );
                                     ~Thread                      ( void );
      static void                     BackgroundCalculator        ( void *unused );
  
  };
  
  #endif
  </code></pre>
  
  <p>Well I can't seem to find the issue.
  After debugging I wanted to "force" aquiring the lock by this code (from the pawn abstract machine):</p>
  
  <pre><code>if (strcmp("/routeme", cmdtext, true) == 0){
      new fromnode = NearestPlayerNode(playerid);
      new start = GetTickCount();
      while(CalculatePath(fromnode,14,playerid+100) == 0){
          printf("0 %d",fromnode);
      }
      printf("1 %d",fromnode);
      printf("Time: %d",GetTickCount()-start);
      return 1;
  }
  </code></pre>
  
  <p>but it keeps endless going on, CalculatePath calls static cell AMX_NATIVE_CALL n_CalculatePath( AMX* amx, cell* params )</p>
  
  <p>That was a bit of surprise. Does anyone maybe see a mistake?</p>
  
  <p>If you need the full source code it is available at:</p>
  
  <p><a href="http://gpb.googlecode.com/files/RouteConnector_174alpha.zip" rel="nofollow">http://gpb.googlecode.com/files/RouteConnector_174alpha.zip</a></p>
  
  <p>Extra info:
  PLUGIN_EXPORT bool PLUGIN_CALL Load 
  gets only executed at startup.</p>
  
  <p>static cell AMX_NATIVE_CALLs
  get only executed when called from a vitrual machine</p>
  
  <p>ProcessTick()
  gets executed every process tick of the application, after it has finished its own jobs it calls this one in the extensions.</p>
  
  <p>For now I only tested the code on windows, but it does compile fine on linux.</p>
  
  <p>Edit: removed linux code to shorten post.</p>