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; //线程IDstring 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;
}
|
转载于:https://www.cnblogs.com/mtcnn/p/9410077.html
boost库 bind/function的使用相关推荐
- Boost库之function的使用
http://www.cnblogs.com/hujian/archive/2009/06/04/1495813.html Boost库的function是一组函数对象包装类的模板,实现了一个泛型的回 ...
- 【Boost】boost库中function和bind一起使用的技巧(一)
1 bind/function 引 (1)头文件 bind函数#include <boost/bind.hpp> function使用头文件#include <boost/funct ...
- 【Boost】boost库中function和bind一起使用的技巧(二)
与 Boost.Function 一起使用 Boost.Bind 当我们把 Boost.Function 与某个支持参数绑定的库结合起来使用时,事情变得更为有趣.Boost.Bind 为普通函数.成员 ...
- 【Boost】boost库中function的用法
要开始使用 Boost.Function, 就要包含头文件 "boost/function.hpp", 或者某个带数字的版本,从"boost/function/funct ...
- c++ Boost库之boost::bind学习
刚开始学c++,就看boost库其实有点小小的不情愿. 团队要求必掌握的Boost库: boost::bind boost::function boost::Signals2 学习前奏:在写关于coc ...
- 函数指针amp;绑定: boost::functoin/std::function/bind
see link: https://isocpp.org/wiki/faq/pointers-to-members function vs template: http://stackoverflow ...
- boost::function模块boost::lambda::bind用法的测试程序
boost::function模块boost::lambda::bind用法的测试程序 实现功能 C++实现代码 实现功能 boost::function模块boost::lambda::bind用法 ...
- Boost:bind绑定的function<>测试程序
Boost:bind绑定的function<>测试程序 实现功能 C++实现代码 实现功能 bind绑定的function<>测试程序 C++实现代码 #include < ...
- Boost库实现线程池学习及线程实现的异步调用
A.Boost线程池实现 参考自: Boost库实现线程池实例 原理:使用boost的thread_group存储多个线程,使用bind方法将要处理的函数转换成线程可调用的函数进行执行:使用队列存储待 ...
最新文章
- 配置 Hdp 4 Window 中的一些问题
- linux正则表达式脚本实例,PowerShell中正则表达式使用例子
- ueditor如何设置上传图片的高度宽度_上百张图片上传并对齐,你加班2小时没搞定,同事简单三步就完成...
- python制作u盘病毒_十行代码--用Python写一个USB病毒!
- Linux_c++ TCP套接字编程函数
- 这款刷爆抖音的玩意儿,竟然是程序员的福音!
- w ndows 那个比较好用,DOS工具箱哪个好用?DOS工具箱盘点
- JFlash烧录不知名的ARM芯片
- 华北水利水电大学历年c语言试题,一百题C语言试题
- 开发linux无线网卡驱动程序,linux-2.6.26下开发marvell 88w8686 wifi驱动 (转)
- 程序流程图N-S图PAD图
- 嵌入式物联网软件开发实战系列(STM32+FreeRTOS)
- 34. 在排序数组中查找元素的第一个和最后一个位置
- Dependency ‘org.mybatismybatisx.x.x‘ not found问题解决方法
- 大学生应如何防止躺平
- redis集群模式是怎么实现的?
- 存储过程语法(MySQL)
- python 24时制 时间转换 ,将12小时制AM \ PM 转换为24小时制
- XStream反序列化漏洞分析
- js操作在当前日期加减(天、周、月、年数)
热门文章
- 新手学习c语言的方法,学习C语言方法“新手必看”
- 震惊! Rightmost Digit 快速幂解决
- 不用数组,解决众数问题(前提 :众数出现的次数必须大于n/2)
- linux两台服务器 同一个地址_【网工玩Linux】搭建开源多运营商(ISP)链路负载均衡器...
- bert 中文 代码 谷歌_ELECTRA中文预训练模型开源,110个参数,性能媲美BERT
- 一道简单却易混淆的高等数学求极限题目
- 嵌入式基于Linux电机控制,基于嵌入式arm+linux平台的直流电机调速控制系统.pdf
- python实例 87,88
- 【youcans 的 OpenCV 例程200篇】125. 形态算法之提取连通分量
- Python小白的数学建模课-10.微分方程边值问题