同步线程
Synchronising threads
如何使用 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
}
}
您正在寻找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
如何使用 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
}
}
您正在寻找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