Boost logo

Boost :

From: Stefan Seefeld (seefeld_at_[hidden])
Date: 2007-03-01 09:38:54


Philippe Vaucher wrote:
> Hello,
>
> Well, the subject says it all ;)
> This question comes because boost::signal's documentation says it's not
> thread safe, but I have trouble finding exactly why it isn't.
>
> Let's look the following example:
>
> Each thread is connected to the same signal, then the signal is emmited, and
> each threads receives the message.... Why would this not be thread safe ?

You use 'thread' in a very lose way here. There is a 'thread' as seen by the
OS, i.e. an execution entity processing some code, and there is are two
'thread' types ('thread1' and 'thread2') you use.

In your description above you seem to refer to those types as 'threads',
not to the execution entities.

The point is, the 'threads of execution' don't 'receive the message', as
the 'slot()' methods are executed in the main thread. However, as you check
the value of 'finished' in other 'threads of execution', you have to protect
access to it by a mutex. For example:

struct thread1
{
  thread1() : finished_(false) {}
  void operator()()
  {
    while (true)
    {
      scoped_lock lock(mutex_);
      if (finished_) return;
    }
  }
  void slot()
  {
    scoped_lock lock(mutex_);
    finished_ = true;
  }

  bool fininshed_;
  mutex mutex_;
};

HTH,
                Stefan

-- 
      ...ich hab' noch einen Koffer in Berlin...

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