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 ?