线程上的连接操作顺序

时间:2014-09-06 17:17:15

标签: c pthreads

我的主程序创建两个线程来同时执行操作。当其中一个线程终止(即完成其任务)时,我想终止另一个。现在,我有以下说明:

pthread_create(&tid1, NULL, operation1, NULL);
pthread_create(&tid2, NULL, operration2, NULL);

pthread_join(tid1, &status1);
pthread_join(tid2, &status2);

if(status1 == 1 ||  status2 == 1)
{
    pthread_kill(tid1, SIGTERM);
    pthread_kill(tid2, SIGTERM);
}

当我加入tid1,然后是tid2时,实际上thread2可以在thread1之前终止,我的程序将直接阻塞直到thread1终止。如何以能够立即检测到一个线程终止的方式执行连接?

0 个答案:

没有答案