我目前有N个线程都是开放的udp / tcp连接。当我收到任何线程中的第一个数据包时,主线程(称为N个线程)需要在N个线程中暂停执行并在恢复N个线程之前做一些工作。
我最初的想法是让所有N个线程使用从主线程等待pthread_cond_broadcast的公共互斥锁。据我了解,所有N个线程将按照调度程序在调用广播时确定的某些顺序顺序恢复执行,因为它们都依赖于相同的互斥锁。但是,我需要它们并行恢复。
这基本上就是我的问题:
主线程:
//create N threads. Each thread is connected to a different
//location but uses the same code
//detect that one of the threads has received a packet
//tell all child threads to pause
pauseThreads();
//do some work on the first packet
resumeThreads();
//tell all child threads to resume
子线程代码:
while(true){
recv() data
//the other N-1 threads should ideally block here,
//since I'd like to process just the
//very first packet
//hopefully pauseThreads() has been called by the main
//thread by here if the first packet has been received.
//All threads should block here until the main thread
//is done processing the first packet. Once it's done
//processing, *firstPacket will be false and the if statement
//can be skipped over
//only one thread should ever access this
if(*firstPacket /*this is a global variable*/ ){
//process first packet
*firstPacket = false;
//the thread that receives the first packet should block here
}
//process same packet data in another format
}
线程需要同时重启的原因是速度是一个问题,我不能等待每个线程逐个完成自己的数据处理。我在if语句中找到了阻塞,但我想不出有效阻止N-1线程的方法。
答案 0 :(得分:0)
获取服务器以将时间戳或序列号添加到udp数据包。
在接收器中,我假设您只有一个网络工作适配器,并且服务器和您之间只有一条路由。否则数据包可能会被重新排序。
所以现在我们需要在进一步处理之前对数据包进行时间/序列处理。为此使用单个线程,然后将剩余的任务分配给工作线程。