title: Pthread Mutex lock unlock by different threads
body: <p>A Naive question ..</p>
  
  <p>I read before saying - "<strong>A MUTEX has to be unlocked only by the thread that locked it.</strong>"</p>
  
  <p>But I have written a program where <strong>THREAD1</strong> locks mutexVar and goes for a sleep. Then <strong>THREAD2</strong> can directly unlock mutexVar do some operations and return.</p>
  
  <p>==> I know everyone say why I am doing so ?? But my question is - Is this a right behaviour of MUTEX ??</p>
  
  <p>==> Adding the sample code</p>
  
  <pre><code>void *functionC()
  {
     pthread_mutex_lock( &amp;mutex1 );
     counter++;
     sleep(10);
     printf("Thread01: Counter value: %d\n",counter);
     pthread_mutex_unlock( &amp;mutex1 );
  }
  
  void *functionD()
  {
     pthread_mutex_unlock( &amp;mutex1 );
     pthread_mutex_lock( &amp;mutex1 );
     counter=10;
     printf("Counter value: %d\n",counter);
  }
  
  int main()
  {
     int rc1, rc2;
     pthread_t thread1, thread2;
  
     if(pthread_mutex_init(&amp;mutex1, NULL))
     printf("Error while using pthread_mutex_init\n");
  
     if( (rc1=pthread_create( &amp;thread1, NULL, &amp;functionC, NULL)) )
     {   
        printf("Thread creation failed: %d\n", rc1);
     }   
  
     if( (rc2=pthread_create( &amp;thread2, NULL, &amp;functionD, NULL)) )
     {   
        printf("Thread creation failed: %d\n", rc2);
     } 
  </code></pre>