Boost logo

Boost :

From: williamkempf_at_[hidden]
Date: 2001-08-01 08:39:26


--- In boost_at_y..., "Scott McCaskill" <scott_at_m...> wrote:
> > No, for all threads. Imagine the following:
> >
> > thread& thrd = thread::self();
> > // actual thread finishes between these instructions
> > if (thrd == thread::self())
> > ...
> >
> > The comparison instruction produces undefined behavior since thrd
> > references an object that's no longer in existence.
> >
>
> Perhaps this is just me being dense, but.. if thrd is on the stack,
I cannot
> see what the problem is here.

I never said it was on the stack. The above obviously wasn't meant
to convey that, since it would be impossible for the thread to
terminate in between the statements if it was.

> In that case, the if statement will always be
> true because each thread has its own stack. If it's not on the
stack, then
> there's a potential programming error. Is the goal to make it
impossible to
> make such an error?

OK, let me give a slightly better, though contrived example.

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.

Bill Kempf


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk