boost库 bind/function的使用
来源:互联网 发布:caffe 添加数据层 编辑:程序博客网 时间:2024/04/28 16:43
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;}
0 0
- boost库 bind/function的使用
- boost库 bind/function的使用
- boost库 bind/function的使用
- boost库 bind/function的使用
- boost::function/bind 的使用
- boost::function与boost::bind 的使用
- boost的库中bind和function使用实例
- C++ boost bind与 boost function的使用
- 关于boost::function与boost::bind函数的使用心得
- 关于boost::function与boost::bind函数的使用心得
- 【Boost】boost库中function和bind一起使用的技巧(一)
- 【Boost】boost库中function和bind一起使用的技巧(二)
- boost::bind和boost::function使用示例
- boost::bind\boost::function
- boost::bind/boost::function
- boost库之bind(与function配合使用)
- C++中std::tr1::function和bind 组件的使用 和 以boost::function和boost:bind取代虚函数
- boost库 bind/function的使用 [大三四八九月实习]
- 我的学生作业3
- static修饰全局变量,局部变量,函数的认识
- 关于java static成员的学习
- RakNet学习(3) -- 介绍
- 1411012118-ny-Binary String Matching
- boost库 bind/function的使用
- POJ2299 求逆序对
- Maven Jetty Plugin 配置指南(翻译)
- [转] 静态分割窗口与多视图实例
- UI控件 - UIImageView
- [LeetCode] Word Ladder
- MySQL的用户与权限管理
- 我也来写博客
- win7下用U盘装ubuntu14.04双系统