[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Thread::join can not exit
From: |
David Sugar |
Subject: |
Re: Thread::join can not exit |
Date: |
Thu, 1 Apr 2004 15:35:43 -0500 |
User-agent: |
KMail/1.5.3 |
I believe this was resolved a different way in 1.1.1/1.1.2. Basically if you
create a "detached" threat (using detach rather than start), it would
automatically self delete as it exits, and without the need to use a "delete
this" in final.
On Thursday 01 April 2004 08:45 pm, Ben.Chen wrote:
> 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
>
> Ben Chen
> Outsourcing
> Shanghai Codex, Inc.
> Tel: 86-21-51314106*1067
> email: address@hidden