I have gone through similar questions on Stackoverflow but still can’t get a good answer:
I am quite puzzled on how this signal/slot is achieved.
Q1: From the following code, sig is connected to two function(Hello() and World()), and it seems that the functions are called in a serialized manner, which also implies that, one function(Hello()) need to be completed before going into another function(World())? => Single thread program
Q2: Are there anyway to enable multi-threaded signal/slot?(=> World() will start instantly, don’t need to wait for Hello() to complete.) Or if it’s not recommended, would you mind tell me why?
Sample codes on Boost website:
struct Hello
{
void operator()() const { std::cout << "Hello";}
};
struct World
{
void operator()() const { std::cout << ", World!" << std::endl;}
};
boost::signal<void ()> sig;
sig.connect(Hello());
sig.connect(World());
sig();
Output:
Hello, World!
Q1:
The calls are serialized. What signals are doing internally is, greatly simplified:
Therefore you don’t want to block in the handlers for long. If you need to do much work you can invoke it from there though, for example by creating a thread for it.
Q2:
boost signals 1 isn’t even thread-safe; signals 2 is, but still does serialized calls. As signals are mostly used for event handling it is common style to not actually do much work in the handlers.
Thus there is no real benefit in calling them ‘in parallel’, the benefits would not in general justify the overhead of the neccessary thread invocations.