bug-commoncpp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Thread::detach() call doesn't return


From: David Sugar
Subject: Re: Thread::detach() call doesn't return
Date: Tue, 1 Apr 2003 22:00:28 -0500
User-agent: KMail/1.5

"detach" is meant to be used in place of "start" to start a constructed thread 
which has no sync/start/signaling sempahore passed as part of it's 
constructor.  If a thread is started, it is normally in the joinable state.  
Using detach as an alternate to start creates a thread context that is 
detached and cannot be joined.  You cannot delete a detached object safely, 
since it may still have an active execution context.  Detached thread objects 
must self delete, typically by using "delete this" in their Final() method.  
In any case, neither detach nor start may be called from a constructor.

On Monday 31 March 2003 01:20 pm, Ian Gough wrote:
> If you are linking in the older code as a dynamic lib, make sure that it
> was compiled with the multithreaded libs.
>
> I don't know if this will help, but I would try fully constructing the
> thread object before attempting to start/detach it. Is there a
> difference between how your program works using start() instead of
> detach()? Try:
>
>  class MyClass1 : public Thread
>  {
>      void run(void)
>      {
>          int i = 0;
>          while(true)
>              if (++i == 0) cout << "Rollover!" << endl;
>      };
>
>      public:
>      MyClass1 (void)
>      {
>      }
>  };
>
>  void main(void)
>  {
>      MyClass1* p1;
>
>      p1 = new MyClass1();
>      p1.start();
>      cout << "MESSAGE" << endl;
>      Thread::sleep( 10000 );
>  }
>
> > -----Original Message-----
> > From:
> > address@hidden
> > [mailto:address@hidden
> > org] On Behalf Of Nacho de los Ríos Tormo
> > Sent: Monday, March 31, 2003 11:00 AM
> > To: address@hidden
> > Subject: Re: Thread::detach() call doesn't return
> >
> >
> > Hello,
> >
> > After a week with no replies, I must face the fact that
> > either nobody knows an
> > answer or, more likely, nobody made could make heads nor
> > tails of a poor (and
> > too long) explanation.
> >
> > My problem is: sometimes, in my Thread subclasses, the moment
> > I call detach()
> > (whether from the constructor or at a later point), the
> > current thread is
> > permanently lost.
> >
> > This only happens when I link in some older code we've got,
> > even if this older
> > code is not called and is left dead.
> >
> > This older code is some older single-thread code that runs fine if:
> >
> > a)compiled as a single thread program
> > b)its original main() function is called from the run()
> >   method of a Thread subclass (though in this case control
> >   won't return to the calling thread).
> >
> > In the example below, when linking the other code, message
> > "MESSAGE" would
> > never get printed; however, not linking that other code,
> > "MESSAGE" would be
> > printed. In both cases, I would get periodic "Rollover!" messages.
> >
> > What so toxic might be lurking in the old code that could
> > make this happen?
> >
> > Thanks for your attention,
> >
> > Nacho de los Rios.
> >
> > ----------------------------------------------------
> >
> > class MyClass1 : public Thread
> > {
> >     void run(void)
> >     {
> >         int i = 0;
> >         while(true)
> >             if (++i == 0) cout << "Rollover!" << endl;
> >     };
> >
> >     public:
> >     MyClass1 (void)
> >     {
> >     detach();
> >     }
> > };
> >
> > void main(void)
> > {
> >     MyClass1* p1;
> >
> >     p1 = new MyClass1();
> >     cout << "MESSAGE" << endl;
> > }
> >
> >
> > _______________________________________________
> > Bug-commoncpp mailing list
> > address@hidden
> > http://mail.gnu.org/mailman/listinfo/bug-> commoncpp
>
> _______________________________________________
> Bug-commoncpp mailing list
> address@hidden
> http://mail.gnu.org/mailman/listinfo/bug-commoncpp





reply via email to

[Prev in Thread] Current Thread [Next in Thread]