|
Boost : |
From: williamkempf_at_[hidden]
Date: 2001-08-01 11:13:42
--- In boost_at_y..., "Alexander Terekhov" <terekhov_at_d...> wrote:
>
> > > 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).
Fistly, Boost.Threads are not POSIX threads. As you've seen, they
are detached in the POSIX implementation, and must be so. Second,
the example I gave is definately contrived (I said so) but it in no
way suggests that the thread is joinable. I won't argue this one any
more. Unless you can provide an implementation _following the
defined Boost.Threads requirements_ that can safely use a reference
return type then there's no further discussion here. Now if you want
to argue the requirements that's a different discussion entirely and
you should start addressing that instead. Please, though, keep in
mind that we are NOT writing a POSIX C++ wrapper here but defining a
C++ threading library from the ground up taking into consideration
*ALL* existing C implementations.
> 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).
Now this points out what might be a problem with the POSIX
implementation that I need to puzzle through. Remember, I'm a Win32
programmer so I'm winging the POSIX implementation.
Bill Kempf
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk