title: Timing overhead binary semaphore vs mutex
body: <p>I have run a sample C++ program on vxWorks platform to test the timing difference between mutex and a binary semaphore. The below program is the prototype</p>
  
  <pre><code>SEM ID semMutex;
  UINT ITER = 10000;
  taskIdOne = TASKSPAWN("t1",TASK_PRIORITY_2,0,8192,0,(FUNCPTR)myMutexMethod,0,0);
  taskIdTwo = TASKSPAWN("t2",TASK_PRIORITY_2,0,8192,0,(FUNCPTR)myMutexMethod,0,0);
  void myMutexMethod(void)
      {
          int i;
          VKI_PRINTF("I'm  (%s)\n",TASKNAME(0) );
          myMutexTimer.start();
          for (i=0; i &lt; ITER; i++)
          {
              MUTEX_LOCK(semMutex,WAIT_FOREVER); 
              ++global;                     
              MUTEX_UNLOCK(semMutex); 
          } 
          myMutexTimer.stop();
          myMutexTimer.show();
      }
  </code></pre>
  
  <p>In the above program there is a contention ( 2 tasks are trying to get the mutex). my timer printed 37.43 ms  for the above program. With the same prototype, binary semaphore program took just 2.8 ms.  This  is understood because binary semaphore is lightweight  and does not have many features like a mutex (priority inversion, ownership etc).  </p>
  
  <p>However, I removed one task and ran the above program ( without contention).  Since there is no contention, the task t1 just gets the mutex , executes the critical section and then releases the mutex. Same with Binary semaphore.<br>
  For the timings, mutex I got 3.35 ms and binary semaphore  4 ms. </p>
  
  <p>I'm surprised to see mutex is faster than binary semaphore when there is no contention.<br>
  Is this expected?  or am I missing something?</p>
  
  <p>Any help is appreciated. ! </p>