如何使用BOOST 信号(一)
信号与插槽-概念
signal :Signals represents callbacks with multiple targets, and are also called publisher or events in similar systems.
slot : Signals are connected to some set of slots, which are callback receivers(also called event targets or subscribers), which are called when the signal is "emitted."
Exp 1 : call a single slot.
#include <boost/signal.hpp>
#include <boost/thread/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
using namespace std;
struct Hello_World
{
void operator ()() const
{
wcout << " Hello World " << endl;
}
};
int main()
{
boost::signal<void ()> sig;
boost::this_thread::sleep(boost::posix_time::milliseconds(5000));
Hello_World hw;
sig.connect(hw);
sig();
return 0;
}
Exp 2 :call multiple slots.
#include <boost/signal.hpp>
#include <boost/thread/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
using namespace std;
using namespace boost;
struct Hello
{
void operator ()() const
{
cout << " HELLO " << endl;
}
};
struct World
{
void operator ()() const
{
cout << " World " << endl;

本文介绍了如何使用BOOST库中的信号(signal)和槽(slot)机制。通过五个示例,展示了如何连接信号到单个或多个槽,如何传递参数并获取结果,包括返回最大值和所有值的集合。示例中涉及了信号的触发、槽的调用顺序、参数传播以及信号结果的组合方式。
最低0.47元/天 解锁文章
223

被折叠的 条评论
为什么被折叠?



