boost库 bind/function的使用

本文介绍如何利用Boost库中的Boost::Function和Boost::Bind来简化自定义线程类的实现过程,通过定义虚拟函数并使用Boost库进行绑定,可以更灵活地控制线程的行为。

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;
}


 



转载于:https://www.cnblogs.com/mtcnn/p/9410077.html

In file included from /usr/include/boost/math/special_functions/detail/round_fwd.hpp:11, from /usr/include/boost/math/special_functions/math_fwd.hpp:29, from /usr/include/boost/math/special_functions/next.hpp:13, from /root/Workspace/openpose/3rdparty/caffe/src/caffe/util/math_functions.cpp:1: /usr/include/boost/math/tools/config.hpp:23:6: warning: #warning "The minimum language standard to use Boost.Math will be C++14 starting in July 2023 (Boost 1.82 release)" [-Wcpp] 23 | # warning "The minimum language standard to use Boost.Math will be C++14 starting in July 2023 (Boost 1.82 release)" | ^~~~~~~ In file included from /usr/include/boost/bind/detail/requires_cxx11.hpp:9, from /usr/include/boost/bind/bind.hpp:24, from /usr/include/boost/bind.hpp:29, from /root/Workspace/openpose/3rdparty/caffe/src/caffe/util/signal_handler.cpp:1: /usr/include/boost/bind.hpp:36:1: note: ‘#pragma message: The practice of declaring the Bind placeholders (_1, _2, ...) in the global namespace is deprecated. Please use <boost/bind/bind.hpp> + using namespace boost::placeholders, or define BOOST_BIND_GLOBAL_PLACEHOLDERS to retain the current behavior.’ 36 | BOOST_PRAGMA_MESSAGE( | ^~~~~~~~~~~~~~~~~~~~ /root/Workspace/openpose/3rdparty/caffe/src/caffe/util/io.cpp: In function ‘bool caffe::ReadProtoFromBinaryFile(const char*, google::protobuf::Message*)’: /root/Workspace/openpose/3rdparty/caffe/src/caffe/util/io.cpp:57:66: error: no matching function for call to ‘google::protobuf::io::CodedInputStream::SetTotalBytesLimit(const int&, int)’ 57 | coded_input->SetTotalBytesLimit(kProtoReadBytesLimit, 536870912); | ^ In file included from /root/Workspace/openpose/3rdparty/caffe/src/caffe/util/io.cpp:2: /usr/local/include/google/protobuf/io/coded_stream.h:407:8: note: candidate: ‘void google::pr
最新发布
03-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值