Safe Cross Streaming Signals / Slot C ++
It seems that the only implementation that provides cross-thread safe for the Signal class and whatever is called in the slot is QT. (Maybe I'm wrong?).
But I cannot use QT in the project I am doing. So how can I ensure that slots are called safely from another thread (like Boost :: signals2)? Is a mutex inside a slot the only way? I think signals2 are protecting themselves, but not what is being done inside the slot.
thanks
a source to share
You can combine boost :: bind and ASIO boost to create cross-flow calls.
# In Thread 2
boost::asio::io_service service;
boost::asio::io_service::work work (service); // so io service won't stop if there is no work
service.run() # starting work thread
# In Thread 1
service.post (boost::bind (&YourClass::function, &yourClassInstance, parameter1, parameter2))
Thread 2 will go into a loop and execute your associated function. I think you can also call Boost :: Signals2 calls in this loop.
But beware: if you are doing cross-thread signaling, make sure the target still exists when called. You can ensure that by removing all connections in your targets' destructor (not in the destructor of their base class, see Signal-Tracked Class )
I don't like Boost :: Signals2 oo because of its very long stack trace and compile time ( blog post ).
a source to share
It is not a signal-slots implementation, exactly, but there is a C ++ implementation of Twisted Deferred that serves a similar purpose for the cross-signal flow mechanism. If someone doesn't come along and post a better solution, it might be worth looking at: http://sourceforge.net/projects/deferred/
a source to share