zoukankan      html  css  js  c++  java
  • boost库 bind/function的使用

    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<<"
    ";
            cout<<i<<"	";
        }
    }
     
    int main()
    {
        boost::function<void()> f;
        f = boost::bind(&ThreadProcess);    
            Thread thread(f, "ThreadTest");
            thread.start();
        sleep(1000*1000);
        return 0;
    }


     



  • 相关阅读:
    [转载]Linux下mail使用技巧
    VTK Online教程大全
    拓扑结构与TopoDS
    三维视图变换与相机模型
    说说DoDataExchange(CDataExchange* pDX)
    Lua Lib在VC下的编译
    建立最简单的OpenCASCADE程序
    构建通用类型 继承 VS 聚合
    【软件】新瓶装老酒 MyCapture
    用std::find查找文件流中的内容
  • 原文地址:https://www.cnblogs.com/mtcnn/p/9410077.html
Copyright © 2011-2022 走看看