|
From: | Ben.Chen |
Subject: | Thread::join can not exit |
Date: | Fri, 2 Apr 2004 09:45:36 +0800 |
Hi,
My environment is g++ 3.2.2 20030222 on Redhat 9.0. CommonCpp2
1.1.0.
I devired a class from TCPSession and run it, then I found
Thread::join can not exit.
class testTCPSession : public TCPSession
{
public:
testTCPSession(...)
:
TCPSession(...)
{
}
protected:
virtual void run()
{
...
}
};
int main()
{
testTCPSession* p = new testTCPSession(...);
p->detach();
p->Thread::join();
return 0;
}
I checked the source code, in Thread::close(),
...
// final can call destructor (that call
Terminate)
final(); // test if this class is self-exiting thread
#ifdef WIN32 if (_self.getKey() == this) #else if (ThreadImpl::_self.getKey() == this) #endif { if(priv)
priv->_tid = 0; joinSem.post(); } ...
If "delete this" in final function, ThreadImpl::_self.getKey() will
not return the key of running thread but the key of main thread, so the
result of compare _expression_ is false, the semaphore is still down, and
hence the join function can not exit.
I changed the code to:
...
#ifdef WIN32
void* key = _self.getKey(); #else void* key = ThreadImpl::_self.getKey(); #endif // final can call destructor (that call
Terminate)
final(); // test if this class is self-exiting thread
if ( key == this ) { if(priv) priv->_tid = 0; joinSem.post(); } ...
It works well.
But I don't know whether I do it correctly. Looking forward to your
feedback. Thanks!
--
Best regards |
[Prev in Thread] | Current Thread | [Next in Thread] |