Boost logo

Boost :

From: Alexander Terekhov (terekhov_at_[hidden])
Date: 2001-08-01 09:06:23


> 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();
}

regards,
alexander.


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