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的使用的更多相关文章

  1. boost之bind,function,signal总结

    boost里的bind,function,signal三个组件都是对用函数做参数(其他算法也用函数做参数),对函数的某一项进行操作. bind主要是对函数参数的作用. function主要是对函数地址 ...

  2. boost中bind的使用

    :first-child { margin-top: 0px; } .markdown-preview:not([data-use-github-style]) h1, .markdown-previ ...

  3. 基于boost的bind与function的一个简单示例消息处理框架

    前两年开始接触boost,boost库真是博大精深:今天简单介绍一下boost中之前用到的的bind与function,感觉挺实用的,分享给大家,我对boost用的也不多,让大家见笑了. 上次文发了一 ...

  4. 借助boost bind/function来实现基于对象编程。

    boost bind/function库的使用: 替换了stl中mem_fun,bind1st,bin2nd等函数.用户注册回调函数需要利用boost/bind转化成库中boost/function格 ...

  5. 函数指针&amp;绑定: boost::functoin/std::function/bind

    see link: https://isocpp.org/wiki/faq/pointers-to-members function vs template: http://stackoverflow ...

  6. boost bind function用法说明

    目录(?)[+] 1 bind/function 引 (1)头文件 bind函数#include <boost/bind.hpp> function使用头文件#include <bo ...

  7. Boost库实现线程池学习及线程实现的异步调用

    A.Boost线程池实现 参考自: Boost库实现线程池实例 原理:使用boost的thread_group存储多个线程,使用bind方法将要处理的函数转换成线程可调用的函数进行执行:使用队列存储待 ...

  8. boost库的安装,使用,介绍,库分类

    1)首先去官网下载boost源码安装包:http://www.boost.org/ 选择下载对应的boost源码包.本次下载使用的是 boost_1_60_0.tar.gz (2)解压文件:tar - ...

  9. C++ Boost库分类总结

    c# 程序员写c++,各种不适应.尤其是被内存操作和几十种字符串类型的转换,简直疯了,大小写转换竟然要手动写代码实现. Boost看介绍不错,也不知道能不能跨平台.过几天要上linux写c++, 也不 ...

随机推荐

  1. Linux Shell基础 Bash常见命令 history、alias命令以及常用快捷键

    概述  shell中常见命令history 历史纪录命令:history 命令格式如下: [root@localhost ~]# history [选项] [历史命令保存文件] -c:清空历史命令: ...

  2. Linux基本命令 vim命令(一)

    vim的三种工作模式 命令模式.输入模式和编辑模式的相互转换,如图 命令模式:使用 Vim 编辑文件时,默认处于命令模式.在此模式下,可以使用上.下.左.右键或者 k.j.h.l 命令进行光标移动,还 ...

  3. C语言:内存字节对齐详解

    转:http://blog.csdn.net/arethe/article/details/2548867 一.什么是对齐,以及为什么要对齐: 1. 现代计算机中内存空间都是按照byte划分的,从理论 ...

  4. awk的输出格式控制:print 和printf

    1.两个函数和若干个内部变量控制awk的输出格式: 两个函数:print和printf 内部变量:OFS:输出的列间隔符,默认为tab;  ORS:输出的行间隔符,默认为\n printf更加自由化, ...

  5. for update排他锁详解

    使用场景: 高并发并且对于数据的准确性很有要求. 落实到mysql就是在事务中使用,只有使用InnoDB时才用,在begin于commit之间使用(只有此引擎支持事务). 本质: 给表或行上个锁以便接 ...

  6. Zabbix3.2 客户端安装

    查看客户端环境: # cat /etc/redhat-release CentOS Linux release (Core) # uname -r -.el7.x86_64 Centos7 客户端: ...

  7. 【P3572】little bird(单调队列+DP)

    一眼看上去这个题就要DP,可是应该怎么DP呢,我们发现,数据范围最多支持O(NlogN),但是这种DP貌似不怎么有,所以应该是O(N)算法,自然想到单调队列优化DP. 然后我们先考虑如果不用单调队列应 ...

  8. hive学习2(Navicat连接hive)

    Navicat连接hive 第一步:win下安装好mysql 第二步:win下安装Navicat 第三步:启动hadoop集群,启动hive 第四步:Navicat连接hive 在第四步中需先配置ss ...

  9. BZOJ 4066 kd-tree 矩形询问求和

    第一次遇见强制在线的题目 每个操作都和前面的ans有关 所以不能直接离线做 在这个问题中 kdtree更像一个线段树在一维单点修改区间询问的拓展一样 如果区间被询问区间完全包含 就不用继续递归 插入时 ...

  10. js数字过大js自动变0

    解决方案: <a onclick="checkDate(${item.billday},'${item.bankcard}',${item.limitmoney/100}) 加上引号变 ...