我有一個線程使用select系統調用來監控串行端口,線程的run函數如下:用select()系統調用語句關閉一個線程?
void <ProtocolClass>::run()
{
int fd = mPort->GetFileDescriptor();
fd_set readfs;
int maxfd=fd+1;
int res;
struct timeval Timeout;
Timeout.tv_usec=0;
Timeout.tv_sec=3;
//BYTE ack_message_frame[ACKNOWLEDGE_FRAME_SIZE];
while(true)
{
usleep(10);
FD_ZERO(&readfs);
FD_SET(fd,&readfs);
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
}
問題是,當我從這個線程退出,使用
delete <protocolclass>::instance();
該程序崩潰與malloc內存損壞glibc錯誤。在用gdb檢查覈心時發現它退出正在處理數據的線程以及錯誤。協議類的析構函數如下所示:
<ProtocolClass>::~<ProtocolClass>()
{
delete [] mpTrackInfo; //delete data
wait();
mPort->ClosePort();
s_instance = NULL; //static instance of singleton
delete mPort;
}
這是由於select?涉及到select時,銷燬對象的語義是否改變?有人可以建議一種乾淨的方式來銷燬涉及select調用的線程。
謝謝
還在爲你的子系統使用單身! – TimW 2009-07-01 07:13:33
是的,但並行工作在其他實現。我是唯一的人,必須處理文檔,測試和開發。並回答我的領導 – rocknroll 2009-07-01 08:57:19