选择()不在线程中工作

roc*_*oll 0 c++ multithreading

我必须监控串口并处理其数据.作为测试程序,我只使用了一个端口的select.运行功能如下:

void <ProtocolClass>::run()
{
    int fd = mPort->GetFileDescriptor();
    fd_set readfs;
    int maxfd=1;
    int res;
    FD_ZERO(&readfs);
   FD_SET(fd,&readfs); 
    struct timeval Timeout;
    Timeout.tv_usec=0;
    Timeout.tv_sec=3;


   //BYTE  ack_message_frame[ACKNOWLEDGE_FRAME_SIZE];
   while(true)
   {
        usleep(10);
        res=select(maxfd,&readfs,NULL,NULL,NULL);
        if(res<0)
           perror("\nselect failed");
        else if( res==0)
                puts("TIMEOUT");
        else if(FD_ISSET(fd,&readfs))
        {//IF INPUT RECEIVED
            qDebug("************RECEIVED DATA****************");
        FlushBuf();
        qDebug("\nReading data into a read buffer");
        int bytes_read=mPort->ReadPort(mBuf,1000);
        mFrameReceived=false;
        for(int i=0;i<bytes_read;i++)
        {
            qDebug("%x",mBuf[i]);
        }



        //if complete frame has been received, write the acknowledge message frame to the port.
        if(bytes_read>0)
        {
            qDebug("\nAbout to Process Received bytes");
            ProcessReceivedBytes(mBuf,bytes_read);
            qDebug("\n Processed Received bytes");
            if(mFrameReceived)
        {
        int no_bytes=mPort->WritePort(mAcknowledgeMessage,ACKNOWLEDGE_FRAME_SIZE);
            }//if frame received
        }//if bytes read > 0
        } //if input received
    }//end while
}
Run Code Online (Sandbox Code Playgroud)

但问题是它似乎没有任何效果.有人可以建议正确的方法来做到这一点.我想使用每个线程选择.这是可行的吗?你能给我一个示例代码吗?我搜索过网,但这些例子只是主要的功能.没有C++特定的例子.我顺便使用Qt线程.

谢谢

War*_*ior 5

我想我知道这是什么问题.

FD_ZERO(&readfs);
FD_SET(fd,&readfs);
Run Code Online (Sandbox Code Playgroud)

上面的行应该在while循环中.因为,select call将重置readFs结构中的'fd'位.因此,下次调用select时,它确实知道要轮询的文件描述符是什么,因为所有都在这里重置.

还应包括stefaanv建议的修正