boost bind/function库的使用:

  替换了stl中mem_fun,bind1st,bin2nd等函数。用户注册回调函数需要利用boost/bind转化成库中boost/function格式的函数。然后调用库的时候就可以回调用户注册的处理函数了。bind也可以将成员函数转换成boost/function指定的函数格式。

#include<iostream>
#include<boost/function.hpp>
#include<boost/bind.hpp> using namespace std;
class Foo
{
public:
void memberFunc(double d,int i,int j)
{
cout << d << endl;
cout << i << endl;
cout << j << endl;
}
}; int main()
{
Foo foo;
//将一种函数类型的接口转换为另一种函数类型的接口。
//将这个成员函数&Foo::memberFuncvoid适配成void f(int)这样的接口。
//成员函数有4个参数this d i j,适配成的函数接口有一个参数。
boost::function<void(int)> fp = boost::bind(&Foo::memberFunc,&foo/*this指针参数*/,0.5,_1,10);
fp(100);
boost::function<void(int,int)> fp = boost::bind(&Foo::memberFunc, &foo, 0.5, _1, _2);
fp(100,200);
return 0;
}

利用boost bind/function实现基于对象的Thread类:

#ifndef _THREAD_H_
#define _THREAD_H_ #include <pthread.h> class
{
public:
typedef boost::function(void()) ThreadFunc;
explicit Thread(const ThreadFunc& func);//阻止隐式转换构造。
void Start();
void Join();
void SetAutoDelete(bool autoDelete);
private:
static void* ThreadRoutine(void* arg);
void Run();
ThreadFunc func_;
pthread_t threadId_;
bool autoDelete_;
}; #endif
//基于对象的方法编写Thread类。
/*
Start()、Join()、ThreadRoutine(void* arg)线程入口函数调用Run()、Run()执行体函数。
typedef boost::function<void()> ThreadFunc;
面向对象的方法中:线程的执行体函数由派生类Run()方法来覆盖积累中的Run()来实现。
基于对象时:线程的执行体函数由构造函数传入ThreadFunc方法。Run()调用这个ThreadFunc();
*/
#include"Thread.h"
#include<iostream>
using namespace std; Thread::Thread(const ThreadFunc& func):autoDelete_(false),func_(func)
{
cout << "Thread..." << endl;
} Thread()::~Thread()
{
cout << "~Thread..." << endl;
} void Thread::Start()
{
pthread_create(&threadId_,NULL,ThreadRoutine,this);
} void Thread::Join()
{
pthread_join(threadId_,NULL);
} void ThreadRoutine(void* arg)
{
Thread* thread = static_cast<Thread*>(arg);
thread->Run();
if (thread->autoDelete_)
delete thread;
return NULL;
} void Thread::SetAutoDelete(bool autoDelete)
{
autoDelete_ = autoDelete;
} void Thread::Run()
{
func_();
}
void ThreadFunc(int count)
{
cout << "threadfunc" << endl;
}
int main(void)
{
Thread t1(boost::bind(ThreadFunc,3));
t1.Start();
t1.Join();
return 0;
}

最新文章

  1. 20150914 异常语句 math的方法 去空格 索引
  2. virtual box 中两个虚拟机 、宿主机 三机互通并且能上外网设置
  3. IC芯片
  4. Javascript基础(2)
  5. (转载)mysql 存在该记录则更新,不存在则插入记录的sql
  6. 消息机制JMS
  7. springmvc环境下使用ajaxfileupload.js进行文件上传
  8. python访问mysql和redis
  9. Java_并发工具包 java.util.concurrent 用户指南(转)
  10. 网络爬虫之html2md
  11. TMS-规划图
  12. easywechat (在thinkphp5中使用easywechat完成微信网页认证)
  13. 博客 first
  14. PAT 乙级 1008 数组元素循环右移问题 (20) C++版
  15. spring注解 @Scheduled(cron = &quot;0 0 1 * * *&quot;)实现定时的执行任务
  16. sonarqube 5.6
  17. Winform重画ComboBox背景色
  18. 解决Qt程序在Linux下无法输入中文的办法(与下文连接)
  19. ubuntu 用法
  20. eclipse 编码设置【转】

热门文章

  1. 换掉7z-zip默认的ico图标,自定义压缩文件图标更美观。
  2. 用python you-get下载视频
  3. Linux下快速搭建测试网站DVWA
  4. centos8平台使用pidstat监控cpu/内存/io
  5. centos8平台安装ansible2.9
  6. Java9系列第8篇-Module模块化编程
  7. java面试之手写单例模式
  8. LinkBlockedQueue的c++实现
  9. pyqy5 程序实例1
  10. spark load data from mysql