top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

what is the difference between spin lock and condition mutex ??

+5 votes

Which one is better for which situation ??

posted Oct 26, 2013 by Vikas Upadhyay

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

1 Answer

+1 vote

Spin lock is useful only for multi-core processor where amount of locking period is very short. Spin lock is example of busy waiting. It means when one thread has taken lock and another thread doesn't enter into sleep mode rather it waits for a small amount of time.

Conditional mutex "Mutex used with conditional variable". In mutex contant polling might consume a set of CPU cycle. So to avoid this wastage, Conditional variable is used with mutex. In conditional mutex one thread signal to another thread on which it was waiting rather keep polling.

Please let me know if I am incorrect.

answer Oct 28, 2013 by Neeraj Mishra
Similar Questions
+2 votes

Here my doubt is about acquire mutex lock.

Here pthread_mutex_t lock ; is also a global variable shared to threads. Accessing of this global variable (lock) will it be shame as accessing of other global variables ? If same, then don't we face same problem what we will face for other global variables ? if not ,how this is discriminated from other global variables ?

+4 votes

There are two threads one takes lock and execute infinite loop. When ever other thread start execution before trying to take lock it unlocks the mutex.

Its a wrong behavior, how can I prevent this.

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

pthread_t callThd_one, callThd_two;
pthread_mutex_t mutexsum;

void *thread_one(void *arg)
   pthread_mutex_lock (&mutexsum);
       printf("\n In thread One \n");

   pthread_mutex_unlock (&mutexsum);
   pthread_exit((void*) 0);

void *thread_two(void *arg)
   pthread_mutex_unlock (&mutexsum);
   //pthread_mutex_lock (&mutexsum);
       printf("\n In thread two \n");

   pthread_mutex_unlock (&mutexsum);
   pthread_exit((void*) 0);

int main (int argc, char *argv[])
   void *status;
   pthread_attr_t attr;

   pthread_mutex_init(&mutexsum, NULL);

   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

   pthread_create(&callThd_one, &attr, thread_one, NULL);
   pthread_create(&callThd_two, &attr, thread_two, NULL);


   pthread_join(callThd_one, &status);
   pthread_join(callThd_two, &status);


Is there any way to sure that mutex should be unlock only by the thread which took lock.

+4 votes

ELF => Executive Linkage Format.

Contact Us
+91 9880187415
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Karnataka INDIA.