Boost logo

Boost :

Subject: Re: [boost] [threads / signals2] Win events replace by boost signals (threadsafe)
From: Ben Pope (benpope81_at_[hidden])
Date: 2015-03-23 06:59:57


On Monday, March 23, 2015 04:55 PM, Fu ji wrote:
> Hello,
>
> I have to change win32 events with something portable so I decided to use
> boost.signals2.
>
> Right now I have something like:
>
> ret = WaitForMultipleObjets{
> switch(ret)
> {
> case Timeout: something; break;
> case Event1: something; break;
> case Event2: something; break;
> }}
>
>
> and I think that the good idea is to replace this with something like that:
>
> for(;;){
> Execute code from timeout, if event occurs interrupt and execute
> code which is responsible for event handling}
>
> but for test I write this:
>
> boost::signals2::signal<void()> signal;
>
> void SendSignal()
> {
> std::cout << "Send signal";
> boost::this_thread::sleep_for(boost::chrono::milliseconds(200));
> signal();
> boost::this_thread::sleep_for(boost::chrono::milliseconds(2000));
> }
>
> int main()
> {
> boost::thread t1(SendSignal);
>
> for (;;)
> {
> std::cout << ".";
> boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
> }
>
> auto c = signal.connect([]()
> {
> std::cout << "Signal receive" << std::endl;
> boost::this_thread::sleep_for(boost::chrono::milliseconds(1000));
> });
>
> return 0;

At this point the thread detaches and the process exits:
http://www.boost.org/doc/libs/1_57_0/doc/html/thread/thread_management.html#thread.thread_management.thread.destructor

You need to call t1.join() before exiting this scope.

For exception safety, you'll probably want to use RAII so that the
thread is joined on the destruction of a scoped object.

Have a look at:
http://www.boost.org/doc/libs/1_57_0/doc/html/thread/ScopedThreads.html#thread.ScopedThreads.strict_scoped_thread

> }
>
> unfortunately this example is not working, what I've done wrong ?
>
>
> Best regards

Ben


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