pthreads-停止当前执行线程,并在特定事件后重新启动它们

125*_*369 2 c pthreads

我正在做一个有关VoIP的项目,我的C代码中有pthreads。我需要启动pthread,并使它们在它们之间进行一些睡眠。现在我的线程正在运行,当我从服务器获得会话结束时,我需要停止正在运行的线程,并从头开始重新启动它们。

我的代码如下所示:

void *recv_thread(void *arg)
{
/*receive the multimedia data and close the recv_thread when there is no more data to receive*/
}


void *send_thread(void *arg)
{
/*send the multimedia data*/
}

send_recv_data()
{

pthread_create(thread2, NULL, send_thread, NULL);
pthread_create(thread3, NULL, recv_thread, NULL);
}

void *first_thread(void *arg)
{
/*if some condition met the start the routine for creation of two threads one for receiving and one for sending data*/
if(cond == TRUE){
send_recv_data();
}

}
main()
{
pthread_create(thread1, NULL, first_thread, NULL);
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,一旦我从另一个用户代理接收到一条消息,即它不再向我发送数据,那么我就需要停止send和recv线程,最后是first_thread,它负责创建另外两个线程。一旦我停止了所有线程,就需要再次重新启动它们。我尝试使用互斥锁和条件变量,但都徒劳无功。

关于如何克服这一问题的任何想法,可能只是一小段简单的代码,都会更有帮助

谢谢

Ant*_*ams 5

为什么需要启动和停止线程?通常最好保持线程运行,并在它们无用的工作时阻止它们在某些对象(例如条件变量)上运行。