|
Boost : |
From: williamkempf_at_[hidden]
Date: 2001-08-01 09:27:10
--- In boost_at_y..., "Alexander Terekhov" <terekhov_at_d...> wrote:
>
> > class foo
> > {
> > public:
> > foo() : owner() { }
> > bool current_thread_is_owner() { return thread() == owner; }
> > private:
> > thread owner;
> > };
> >
> > foo * instance;
> >
> > void thrd1()
> > {
> > instance = new foo;
> > }
> >
> > void thrd2()
> > {
> > if (foo->current_thread_is_owner())
> > do_something();
> > }
> >
> > Ignoring all synchronization problems intentionally left out and
the
> > fact that this is a very contrived example you should get the
idea.
>
> ignoring all synchronization problems you could also do it
> via "static thread& thread::current()" which is much more
> clear, IMHO:
>
> 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 Kempf
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk