|
Boost : |
From: Alexander Terekhov (terekhov_at_[hidden])
Date: 2001-08-01 10:25:41
> > class foo
> > {
> > public:
> > foo() : owner( thread::current() ) { }
> > bool current_thread_is_owner() { return thread::current() ==
> owner; }
> > private:
> > thread& owner;
> > };
> >
> > foo * instance;
> >
> > void thrd1()
> > {
> > instance = new foo;
> > }
> >
> > void thrd2()
> > {
> > if (foo->current_thread_is_owner())
> > do_something();
> > }
>
> No, you can't do it this way, because thrd1 may terminate long before
> the call to current_thread_is_owner() in thrd2 is ever invoked. This
> would leave you with a reference to an object that no longer exists.
Bill, the owner thread is supposed to be joinable (not detached);
the thread object will exist after thrd1 termination (and even
after owner.join() <- done by some other thread; that will
reclaim id/handle; but not the C++ thread object itself).
thread::operator== should just compare this and &rhs pointers
(to noncopyable thread objects). as for detached threads -- you
cannot detach a thread (object) which was not allocated dynamically
and until you have some reachable references/pointers to it hanging
around (like foo::owner); detaching thread (object) in such situation
(and without some mechanism to perform global refs/ptrs reset/cleanup
on detached thread termination) is simply a BUG (same problem as the
destruction of joinable thread object when some reachable refs/ptr
to it are still hanging around).
regards,
alexander.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk