c++boostasynchronousexecutionboost-signals

Are boost::signals slots called synchronously or asynchronously?


Can anyone tell me are boost::signals slots called synchronously or asynchronously?

For example I have this piece of code:

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();

cout << "Foo";

How does the execution thread work? Does the execution wait for Hello() and World() to execute and just after that "Foo" is printed or does it call them asynchronously(printing "Foo" and calling Hello() and World() execute in an undefined order)?


Solution

  • In Boost.Signals slots are called synchronously and slots connected to the same signal are called in the order in which they were added. This is true also of the thread-safe variant, Boost.Signals2