如何使两个线程互相等待,直到它们使用pthreads完成一个循环?
void* th1Fn()
{
while(1)
{
//do something
printf("I'm done");
//signal that i'm done
//wait for thread2 so that I can repeat the cycle
}
}
void* th2Fn()
{
while(1)
{
//do something
printf("I'm done");
//signal that i'm done
//wait for thread1 so that I can repeat the cycle
}
}
答案 0 :(得分:1)
您正在寻找pthread_barrier_wait
:http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_barrier_wait.html
以下是此功能的记录示例:http://man7.org/tlpi/code/online/dist/threads/pthread_barrier_demo.c.html