0

I am working on a code which creates three thread . Now using pthread_mutex how can i synchronize them? Lets say i have this type of code:-

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

void *function1(void *ptr)
{

   do something on a buffer;
}

void *function2(void *ptr)
{
  do samething else but ob the same buffer;
 }

void *function(void *ptr)
{
  do samething else again on buffer;
}


main()
{ 
   pthread_t t1,t2,t3);
   int a,b,c;
   a= creates a thread using pthread_create(&t1,NULL,func1,NULL);
   b= creates a thread using pthread_create(&t2,NULL,func2,NULL);
   c= creates a thread using pthread_create(&t3,NULL,func1,NULL);

 and after that pthread_join wait till all the threads gets finished;
}

But as you can see when you do such thing all threads starts simultaneously and the result is not as expected . Now how can we use pthread_mutex to lock and unlock the same buffer so that at a time only one thread can work on it? Also i want t1 to come first then t2 and after that t3. How can it be done basically i have to set priority ?

user2714823
  • 607
  • 5
  • 15
  • 29
  • Something tells me that if opened *any* manual or looked for examples on the internet you would have found thousands of code examples and explanations for this... – Kerrek SB Aug 25 '13 at 11:23
  • As @KerrekSB said, first search for answers, then ask question. Your question is pretty basic, you will find its answer even if you read the usage of pthread mtexes. – 0xF1 Aug 25 '13 at 11:30
  • I read about pthread_mutex_lock() but when i implement the output is not as expexted. I want to call t1 then t2 and then t3 but when i compile the code the order is random . How can i getrid of this? – user2714823 Aug 25 '13 at 17:06

1 Answers1

2

See this example: http://sourcecookbook.com/en/recipes/70/basic-and-easy-pthread-mutex-lock-example-c-thread-synchronization

You must do a little bit of reading, but it's pretty easy. You must declare and initialize the mutex: pthread_mutex_t myMutex; pthread_mutex_init(&myMutex, NULL);

and to use it:

pthread_mutex_lock(&myMutex);

// use the buffer here

pthread_mutex_unlock(&myMutex);

Also don't forget to cleanup: pthread_mutex_destroy(&myMutex);

Gabi Turliu
  • 226
  • 1
  • 5
  • I read about pthread_mutex_lock() but when i implement the output is not as expexted. I want to call t1 then t2 and then t3 but when i compile the code the order is random . How can i getrid of this? – user2714823 Aug 25 '13 at 17:05
  • @user2714823 You can't. Threads execute whenever they can, which is, as you say, random. – Boann Aug 25 '13 at 17:37
  • in the above example i want first thread1 finishes then thread2 and thread3 . Is it possible? – user2714823 Aug 25 '13 at 17:47
  • Why do you need threads then? You can just call the methods. If, however, you still want to call the threads in a sequential manner(lets say for experimental reasons) you can use pthread_join to wait for thread termination. Basically you start t1, then you wait for it with pthread_join. Then you start t2 and use pthread_join again etc. – Gabi Turliu Aug 25 '13 at 17:52