Small example of Boost.signals2 signals/slots

 Signal slots are an important part of the Qt framework. They are mainly used to decouple a set of classes that cooperate with each other, and are very convenient to use. A colleague in the project introduced a third-party signal-slot mechanism. In fact, Boost itself has signals/slots, and Boost's modules are relatively more stable.

  signals2 implements a thread-safe observer pattern based on signals, another library in Boost. One of the more important operation functions in signal is connect, which connects the slot to the signal; the slot can be any callable object, including function pointers, function objects, and their bind/lambda expressions and function objects. The connect function will return a connection object, which represents the connection relationship between the signal and the slot. The connection object can more flexibly handle the connection and disconnection between the signal and the slot function.

  Here is a small example using Boost signals/slots:  

#include "stdafx.h"
#include <iostream>
#include <boost/signals2.hpp>
#include <boost/bind.hpp>

using namespace std;
using namespace boost::signals2;

void slots1()
{
    cout<<"slots1 called"<<endl;
}

void slots2()
{
    cout<<"slots2 called"<<endl;
}

class A
{
public:
    static int staticMemberFunc(int param)
    {
        cout<<"A::staticMemberFunc called, param: "<<param<<endl;
        return 0;
    }
    int memberFunc(int param)
    {
        cout<<"A::memberFunc called, param: "<<param<<endl;
        return 0;
    }
};

intmain()
{
    boost::signals2::signal<void()> sig;
    boost::signals2::signal<int(int)> sig2;

    A a;
    connection c1 = sig.connect(&slots1);
    connection c2 =sig.connect(&slots2);
    cout<<"First call-------------------"<<endl;
    sig ();
    if (c2.connected())
    {
        c2.disconnect();
    }
    cout<<"Second call-------------------"<<endl;
    sig ();

    connection c3 =sig2.connect(&A::staticMemberFunc);// Bind member function
    connection c4 =sig2.connect(boost::bind(&A::memberFunc, &a, _1));// bind static member function

    cout<<"Return code is: "<<*sig2(44)<<endl;// Only the return value of the last called slot can be returned
    return 0;
}

//Output:
First call-------------------
slots1 called
slots2 called
Second call-------------------
slots1 called
A::staticMemberFunc called, param: 44
A::memberFunc called, param: 44
Return code is: 0

 Note that using the dereferencing operator * gets only the return value of the last called slot, if you need to know the return value of each slot function you need to use a combiner.

  Boost's signal/slot function can only be executed synchronously when the signal is triggered, and there is no asynchronous interface like Qt. The implementation of Qt asynchronous is actually to push the signal to a queue, and then a unified thread handles the slot function corresponding to the signal. Of course, you can also encapsulate the asynchronous signal/slot mechanism based on this principle, but in that case you should need to open another thread.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325643495&siteId=291194637