私は3つのスレッドを作成するコードに取り組んでいます。pthread_mutex を使用して、どうすればそれらを同期できますか? このタイプのコードがあるとしましょう:-
#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;
}
しかし、ご覧のとおり、このようなことを行うと、すべてのスレッドが同時に開始され、期待どおりの結果が得られません。では、pthread_mutex を使用して同じバッファーをロックおよびロック解除し、一度に 1 つのスレッドしか処理できないようにするにはどうすればよいでしょうか? また、t1が最初に来て、次にt2が来て、その後t3が来るようにしたい. 基本的に優先度を設定する必要がありますか?