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. jQuery:自学笔记(1)——基础入门

    jQuery:自学笔记(1)——基础入门 认识JQuery 1.jQuery概述 jQuery是一个快速.小巧 .功能丰富的JavaScript函数库.它可以实现“写的少,做的多”的目标. jQuer ...

  2. 财经世界(2)A股B股和H股

    在发行过程中,公司通过章程给不同股份形式赋予不同的权益,使公司股份出现A股.B股等形式.我国上市公司的股票有A股.B股.H股.N股.S股等的区分.这一区分的主要依据股票的上市地点和所面对的投资者而定. ...

  3. git操作整理

    昨天手残 然后在GitHub for windows 上点了revert 然后就给重置了 更手残的是又给同步了 .  但是 GitHub 会保留之前的版本 . 只要删掉本次修改就可. 解决方案:  g ...

  4. Linux Shell基础 管道符和grep命令

    概述 管道符:管道符使用"丨"代表.如"命令1丨命令2".表示命令 1 的正确输出作为命令 2 的操作对象.命令 1 必须有正确输出,而命令 2 必须可以处理命 ...

  5. Python mysql表数据和json格式的相互转换

    功能: 1.Python 脚本将mysql表数据转换成json格式 2.Python 脚本将json数据转成SQL插入数据库 表数据: SQL查询:SELECT id,NAME,LOCAL,mobil ...

  6. Go 功能测试与性能测试

    1.功能测试 calcTriangle.go // 需要被测试的函数 func calcTriangle(a, b int) int { return int(math.Sqrt(float64(a* ...

  7. eclipse oxygen 版本(即为4.7版本)打开 could not create the java virtual machine问题

    1.问题: could not create the java virtual machine 2.解决办法: 找到eclipse目录下的eclipse.ini文件.打开找到以下内容: -vmargs ...

  8. 【P2401】不等数列(DP)

    这个题乍一看就应该是DP,再看一眼数据范围,1000..那就应该是了.然后就向DP的方向想,经过对小数据的计算可以得出,如果我们用f[i][j]来表示前i个数有j个是填了"<" ...

  9. 加和求不同的组合方式数目(dp)

    描述 有n个正整数,找出其中和为t(t也是正整数)的可能的组合方式.如: n=5,5个数分别为1,2,3,4,5,t=5: 那么可能的组合有5=1+4和5=2+3和5=5三种组合方式. 输入 输入的第 ...

  10. 在 Students 的 Index 页面增加列标题链接(排序),分页,过滤和分组功能

    3-1  在 Students 的 Index 页面增加列标题链接 为 Index 页面增加排序的功能,我们需要修改 Student 控制器的 Index 方法,还需要为 Student 视图增加代码 ...