#include"iostream"
#include"boost/signals.hpp"
#include"unistd.h"
using namespace std;
void my_slot(){
cout<<"my_slot()"<<endl;
}
void my_slot2(int i){
cout<<"my_slot2():i="<<i<<endl;
}
void my_slot3(){
cout<<"my_slot3()"<<endl;
}
int main(int argc, char** argv)
{
//声明信号类型
boost::signal<void()> sig;
boost::signal<void(int)> sig2;
//绑定一个具体的槽函数
sig.connect(1,my_slot);//前面的参数是让槽函数执行先后
sig.connect(0,my_slot3);
sig2.connect(my_slot2);
//发信号
int i =0;
while(true)
{
sleep(1);
sig2(10);
sig();
if(i == 10)
{
break;
}
else if(i == 5)
{
sig.disconnect_all_slots(); //中途取消sig的发送,sig2继续发送
}
i++;
}
return 0;
}
CmakeLists.txt:
cmake_minimum_required(VERSION 2.6)
project(boost_signal)
find_package(Boost REQUIRED COMPONENTS
signals)
set(boost_signal_SRCS boost_signal.cpp main.cpp)
add_executable(boost_signal ${boost_signal_SRCS})
target_link_libraries(boost_signal ${Boost_LIBRARIES})
执行结果: