|
Boost : |
From: Philippe Vaucher (philippe.vaucher_at_[hidden])
Date: 2007-03-01 09:23:19
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 ?
Of course in this code the issue would be that I don't mutex the "finished"
variable but that's another story.
#include <iostream>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/signals.hpp>
struct thread1
{
void operator()() { finished = false; while(!finished); }
void slot() { finished = true; }
bool finished;
};
struct thread2
{
void operator()() { finished = false; while(!finished); }
void slot() { finished = true; }
bool finished;
};
int main()
{
boost::signal<void ()> sig;
thread1 t1;
thread2 t2;
sig.connect(boost::bind(&thread1::slot, &t1));
sig.connect(boost::bind(&thread2::slot, &t2));
boost::thread trd1(boost::ref(t1));
boost::thread trd2(boost::ref(t2));
usleep(10);
sig();
trd1.join();
trd2.join();
return 0;
}
Thank you for insight.
Philippe
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk