boost库 bind/function的使用
Boost::Function 是对函数指针的对象化封装,在概念上与广义上的回调函数类似。相对于函数指针,function除了使用自由函数,还可以使用函数对象,甚至是类的成员函数,这个就很强大了哈
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <iostream>
using namespace std;
class TestA
{
public:
void method()
{
cout<<"TestA: method: no arguments"<<endl;
}
void method(int a, int b)
{
cout<<"TestA: method: with arguments"
<<"value of a is:"<<a
<<"value of b is "<<b <<endl;
}
};
void sum(int a, int b)
{
int sum = a + b;
cout<<"sum: "<<sum<<endl;
}
int main()
{
boost::function<void()> f;
TestA test;
f = boost::bind(&TestA::method, &test);
f();
f = boost::bind(&TestA::method, &test, 1, 2);
f();
f = boost::bind(&sum, 1, 2);
f();
}
2. 应用:Thread封装
在实现自定义的线程类时,曾经这么干过:定义虚函数run(),用户自定义的CustomThread::Thread后,自己实现run()函数就OK了。 当时觉得这么做也不错。
现在有了boost::function/boost::bind我们可以这么干:
定义一个线程类:
.h文件
#include <pthread.h>
#include <string>
#include <boost/function.hpp>
#include <boost/bind.hpp>
using namespace std;
class Thread
{
typedef boost::function<void()> ThreadFun;
public:
Thread(const ThreadFun& threadFun,const string& threadName = string());
pid_t getThreadId();
string getThreadName();
int start();
private:
static void* startThread(void* thread);
private:
pthread_t m_thread; //线程句柄
pid_t m_tid; //线程ID
string m_strThreadName; //线程名称
bool m_bStarted; //线程是否启动
ThreadFun m_func; //线程处理函数
};
.cpp
#include "thread.h"
Thread::Thread(const Thread::ThreadFun& threadFun, const string& threadName):
m_func(threadFun), m_strThreadName(threadName)
{
}
int Thread::start()
{
m_tid = pthread_create(&m_thread, NULL, &startThread, this);
return 0;
}
void* Thread::startThread(void* obj)
{
Thread* thread = static_cast<Thread*>(obj);
thread->m_func();
return NULL;
}
pid_t Thread::getThreadId()
{
return m_tid;
};
string Thread::getThreadName()
{
return m_strThreadName;
}
void ThreadProcess()
{
int count = 100;
for (int i = 0; i < count; i++)
{
if (i % 10 == 0)
cout<<"\n";
cout<<i<<"\t";
}
}
int main()
{
boost::function<void()> f;
f = boost::bind(&ThreadProcess);
Thread thread(f, "ThreadTest");
thread.start();
sleep(1000*1000);
return 0;
}
|
boost库 bind/function的使用的更多相关文章
- boost之bind,function,signal总结
boost里的bind,function,signal三个组件都是对用函数做参数(其他算法也用函数做参数),对函数的某一项进行操作. bind主要是对函数参数的作用. function主要是对函数地址 ...
- boost中bind的使用
:first-child { margin-top: 0px; } .markdown-preview:not([data-use-github-style]) h1, .markdown-previ ...
- 基于boost的bind与function的一个简单示例消息处理框架
前两年开始接触boost,boost库真是博大精深:今天简单介绍一下boost中之前用到的的bind与function,感觉挺实用的,分享给大家,我对boost用的也不多,让大家见笑了. 上次文发了一 ...
- 借助boost bind/function来实现基于对象编程。
boost bind/function库的使用: 替换了stl中mem_fun,bind1st,bin2nd等函数.用户注册回调函数需要利用boost/bind转化成库中boost/function格 ...
- 函数指针&绑定: boost::functoin/std::function/bind
see link: https://isocpp.org/wiki/faq/pointers-to-members function vs template: http://stackoverflow ...
- boost bind function用法说明
目录(?)[+] 1 bind/function 引 (1)头文件 bind函数#include <boost/bind.hpp> function使用头文件#include <bo ...
- Boost库实现线程池学习及线程实现的异步调用
A.Boost线程池实现 参考自: Boost库实现线程池实例 原理:使用boost的thread_group存储多个线程,使用bind方法将要处理的函数转换成线程可调用的函数进行执行:使用队列存储待 ...
- boost库的安装,使用,介绍,库分类
1)首先去官网下载boost源码安装包:http://www.boost.org/ 选择下载对应的boost源码包.本次下载使用的是 boost_1_60_0.tar.gz (2)解压文件:tar - ...
- C++ Boost库分类总结
c# 程序员写c++,各种不适应.尤其是被内存操作和几十种字符串类型的转换,简直疯了,大小写转换竟然要手动写代码实现. Boost看介绍不错,也不知道能不能跨平台.过几天要上linux写c++, 也不 ...
随机推荐
- LeetCode:数据库技术【175-178】
LeetCode:数据库技术[175-178] LeetCode已经刷完200道题目,但这只是开始,下一段时间,仍然把刷题作为重点,争取再次完成200道,本篇博客将会带大家熟悉一些数据库面试题,从简单 ...
- qt的登录设置(转)
1.下面添加代码来实现使用用户名和密码登录,这里只是简单将用户名和密码设置为了固定的字符串,如果以后学习了数据库,还可以通过读取数据库来获取用户名和密码.到logindialog.cpp文件中将登录按 ...
- vi高级命令集锦
.交换两个字符位置 xp .上下两行调换 ddp .把文件内容反转 :g/^/m0/ (未通过) .上下两行合并 J .删除所有行 dG .从当前位置删除到行尾 d$ .从当前位置复制到行尾 y$ 如 ...
- Django-实现图片验证码
Django实现图片验证码 Python生成随机验证码,需要使用PIL模块. pip3 install pillow 1.基本使用 1. 1 创建图片 from PIL import Image im ...
- jQuery带缩略图焦点图插件
在线演示 本地下载
- 20145239《网络对抗》- 逆向及Bof基础实践
1 逆向及Bof基础实践说明 1.1 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件.该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串.该程序同 ...
- 【P2422】良好的感觉(单调栈优化DP//奇怪的暴力)
话说正解是单调栈优化DP,然而貌似根据某种玄学的推算,这个题暴力出解貌似也是可以的.首先,我们枚举所有的点作为最小点,然后横向展开,遇到更小的就停止...然后再操作一下,看上去时间O(N^2),然而由 ...
- tensorFlow 神经网络2
learnrate 太大容易跑飞,设置激活函数 可以一定程度上增加learnrate,不跑飞 self.saver = tf.train.Saver() 和 self.init_variable = ...
- Flume架构及运行机制
flume 作为 cloudera 开发的实时日志收集系统,受到了业界的认可与广泛应用.Flume 初始的发行版本目前被统称为 Flume OG(original generation),属于 clo ...
- QT 利用ListWidget 和 StackedLayout 配合实现 分页 选项
1. 如图, 左边为listwidget,右边为StackedLayout, 通过listwidget的不同选项,可以使右边的不同页显示出来. 2. dialog.h #ifndef DIALOG_H ...