top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

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

+5 votes
542 views

Which one is better for which situation ??

posted Oct 26, 2013 by Vikas Upadhyay

Share this question
Facebook Share Button Twitter Share Button LinkedIn 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);
   while(!sleep(1))
       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);
   while(!sleep(1))
       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_init(&attr);
   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

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

   pthread_attr_destroy(&attr);

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

   pthread_mutex_destroy(&mutexsum);
   pthread_exit(NULL);
}

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

+4 votes

ELF => Executive Linkage Format.

...