0

我检查了相关帖子,但仍然无法得到答案。请高手帮帮我。我基本上有两个线程,一个线程从以太网接收数据,另一个处理接收到的数据。为了获得最大性能,ThreadRecv recv 第 n 帧和 ThreadCompute Process (n-1)th 帧。我记录了时序关系,发现如果事件在Wait函数之前发出信号,是不会得到事件的。

这是粗略的代码:

st_threads::st_threads():Event_ComputeDone(0,0),Sem_KillThread(0,2),
        Sem_PauseThread(0,2),Sem_ResumeThread(0,2),Event_RecvDone(0,0),
        Event_RecvTerminated(0,0),Event_RecvPaused(0,0),Event_RecvStart(0,0),//make it automatic
        Event_ComputeTerminated(0,0),Event_ComputePaused(0,0)
{
    ping_num=0;
    thread_state=stopped;
    step=1;
    //threadHandles[0]=0;
    //threadHandles[1]=0;
}

UINT st_threads::ThreadRecv(LPVOID pParam)
{
    st_threads *pThread=(st_threads*)pParam; //get the pointer
    hptime t0;
    double dtime,dtime0;
    while(1)
    {
        dtime0=t0.gettime();
        //check if terminate the thread

        if(pThread->ping_num)
        WaitForSingleObject(pThread->Event_RecvStart,INFINITE);


        //realtime processing
        dtime=t0.gettime();
        switch(pThread->work_mode)
        {
        case realtime_mode:
            //if(pThread->ECP->IsAlive())
            {
                pThread->frame_len=pThread->ECP->RecvDataFrame(pThread->rawbuf,MAX_RAW_BUF_SIZE); //receive a data frame into a buffer
                if(pThread->frame_len<192)
                {
                    pThread->frame_len=pThread->ECP->RecvDataFrame(pThread->rawbuf,MAX_RAW_BUF_SIZE); //receive a data frame into a buffer
                }
                if(pThread->frame_len<0)
                {
                    TRACE("recv error\n");
                }
                pThread->pdatalist->push_back(raw_data(pThread->frame_len,pThread->rawbuf,pThread->ping_num));
                pThread->praw_frame[pThread->ping_num%2]=--pThread->pdatalist->raw_buf.end();
            }
            break;

        default:break;
        }

        pThread->ping_num++;
        pThread->Event_RecvDone.SetEvent(); //let compute to start

        pThread->threadtime.time_ThreadRecv=dtime0;
        pThread->threadtime.dt_recv=t0.gettime()-dtime;
        pThread->threadtime.dt_ThreadRecv=t0.gettime()-pThread->threadtime.time_ThreadRecv;
        //
        //TRACE("RECV:ping#=%d\n",pThread->ping_num);

    }

    AfxEndThread(0);
    return 0;
}

UINT st_threads::ThreadCompute(LPVOID pParam)
{
    st_threads *pThread=(st_threads*)pParam;

    int prev_frame_time=0,data_mode;
    double time_elapsed,dt,dtime0;
    hptime t0;
    while(1)
    {
        //WaitForSingleObject(pThread->Event_ComputeStart,INFINITE);
        dtime0=t0.gettime();

        WaitForSingleObject(pThread->Event_RecvDone,INFINITE);
        pThread->Event_RecvStart.SetEvent(); //let recv start at the same time as computing


        //realtime processing
        if(pThread->ping_num)
        {
            dt=t0.gettime();
            pThread->dispatch_packet(pThread->praw_frame[(pThread->ping_num-1)%2],&data_mode,pThread->ping_num-1);
            pThread->threadtime.dt_comp=t0.gettime()-dt;

                pThread->threadtime.dt_frame_interval=t0.gettime()-dtime0;

        }
        //else Sleep(100);
        pThread->Event_ComputeDone.SetEvent();

        pThread->threadtime.time_ThreadCompute=dtime0;
        pThread->threadtime.dt_ThreadCompute=dt=t0.gettime()-pThread->threadtime.time_ThreadCompute;
        if(pThread->debug_mode) pThread->logtime(pThread->ping_num,data_mode);
        //TRACE("COMP:ping#=%d, frame rate: %d\n",pThread->ping_num,int(1000.0/dt));
        //
    }
    AfxEndThread(0);
    return 0;
}

程序会记录时间,部分输出是这样的:

0177 15 364486101   23  23  364486099   23  21  23
0178 15 364486124   23  23  364486122   23  21  23
0179 15 364486147   23  23  364486145   23  20  23
0180 15 364486170   23  23  364486167   23  21  23
0181 15 364486193   23  23  364486190   23  21  23
0182 15 364486216   21  21  364486213   23  20  21
0183 15 364486238   23  23  364486236   23  21  23
0184 15 364486261   23  23  364486259   23  21  23
0185 15 364486284   22  22  364486282   23  21  22
0186 15 364486307   23  23  364486305   23  21  23
0187 15 364486330   23  23  364486328   23  21  23
0188 15 364486353   23  23  364486351   23  20  23
0189 15 364486376   23  22  364486374   23  20  23
0190 15 364486399   23  23  364486396   23  21  23
0191 15 364486422   23  23  364486420   23  21  23
0192 15 364486445   23  23  364486442   23  21  23
0193 15 364486468   23  23  364486465   23  20  23
0194 15 364486491   22  22  364486488   23  20  22
0195 15 364486513   23  23  364486511   23  21  23
0196 15 364486536   23  23  364486534   23  21  23
0197 15 364486559   22  22  364486557   23  21  22
0198 15 364486582   23  23  364486580   23  21  23
0199 15 364486605   23  23  364486603   23  20  23
0200 15 364486628   23  23  364486625   25  23  23
0200 15 364486651   23  23  364486625   25  23  23
0201 15 364486674   24  22  364486650   26  25  24
0202 15 364486698   25  22  364486676   25  25  25
0203 15 364486723   26  23  364486701   25  25  26
0204 15 364486749   25  23  364486726   25  25  25
0205 15 364486774   24  22  364486751   25  25  24
0206 15 364486798   26  22  364486776   26  26  26
0207 15 364486825   24  21  364486802   25  25  24
0208 15 364486849   25  22  364486827   25  25  25
0209 15 364486874   25  22  364486852   25  25  25
0210 15 364486900   25  22  364486877   25  25  25
0211 15 364486925   25  22  364486902   26  25  25
0212 15 364486950   25  22  364486928   25  25  25
0213 15 364486975   26  22  364486953   26  26  26
0214 15 364487001   26  22  364486979   26  26  26
0215 15 364487027   24  22  364487004   25  25  24
0216 15 364487051   25  22  364487029   25  25  25
0217 15 364487076   29  22  364487054   29  29  29
0218 15 364487106   26  23  364487083   26  26  26
0219 15 364487132   25  22  364487109   26  26  25

第一列是 ping 号,第二列不相关,第三列是 ThreadCompute 的绝对时间,第四列是 ThreadCompute 的持续时间,第五列不相关,第六列是 ThreadRecv,第七列是 threadRecv 的持续时间。请注意,我们得到两个相同的输出,ping 数为 200。这表明 threadrecv 比 ThreadCompute 慢一点。recvStart 比 Wait 函数更早发出,并且没有被接收到。

4

1 回答 1

0

这是一个完全错误的设计。

 WaitForSingleObject(pThread->Event_RecvDone,INFINITE);
 pThread->Event_RecvStart.SetEvent(); //let recv start at the same time as computing

您等待接收数据包(由 ping_num 标识)并告诉 Receiver 线程继续接收,然后开始处理数据包。接收者线程可以在计算线程完成处理刚刚收到的数据包之前增加 ping_num 。这在多线程环境中是完全可能的。

我认为您应该重新设计接收/计算过程的逻辑。最好使用列表或队列将接收到的数据包存储在 Receiver 线程中,并在 Computing 线程中提取它们。当然,您必须使用 Mutex 或其他方式来保护列表/队列。

于 2013-11-05T03:39:49.270 回答