Boost库实现线程池实例

  1. #ifndef MY_TASK_QUEUE_H   
  2. #define MY_TASK_QUEUE_H   
  3. #include <queue>   
  4. #include <boost/thread.hpp>   
  5. #include <boost/noncopyable.hpp>   
  6. #include <boost/function.hpp>   
  7. //using namespace boost;   
  8. //using namespace std;   
  9. typedef boost::function<void(void)> my_task;  
  10. //任务队列   
  11. class Task_queue:boost::noncopyable  
  12. {  
  13. private:  
  14.     std::queue<my_task> my_queue;  
  15.     boost::condition_variable_any cond;  
  16.     boost::mutex my_mutex;  
  17. public:  
  18.     void push_task(const my_task & task_func)  
  19.     {  //加上互斥锁   
  20.         boost::unique_lock<boost::mutex> lock(my_mutex);  
  21.         my_queue.push(task_func);  
  22.         //通知其他线程启动   
  23.         cond.notify_one();  
  24.     }  
  25.     my_task get_task(){  
  26.         //加上互斥锁   
  27.         boost::unique_lock<boost::mutex> lock(my_mutex);  
  28.         if(my_queue.size()==0)  
  29.         {  
  30.             //如果队列中没有任务,则等待互斥锁   
  31.             cond.wait(lock);  
  32.         }  
  33.         //指向队列首部   
  34.         my_task task(my_queue.front());  
  35.         //出队列   
  36.         my_queue.pop();  
  37.         return task;        
  38.     }  
  39.     int get_size()  
  40.     {  
  41.         return my_queue.size();  
  42.     }  
  43. };  
  44.   
  45. #endif    
#ifndef MY_TASK_QUEUE_H
#define	MY_TASK_QUEUE_H
#include <queue>
#include <boost/thread.hpp>
#include <boost/noncopyable.hpp>
#include <boost/function.hpp>
//using namespace boost;
//using namespace std;
typedef boost::function<void(void)> my_task;
//任务队列
class Task_queue:boost::noncopyable
{
private:
    std::queue<my_task> my_queue;
    boost::condition_variable_any cond;
    boost::mutex my_mutex;
public:
    void push_task(const my_task & task_func)
    {  //加上互斥锁
        boost::unique_lock<boost::mutex> lock(my_mutex);
        my_queue.push(task_func);
        //通知其他线程启动
        cond.notify_one();
    }
    my_task get_task(){
        //加上互斥锁
        boost::unique_lock<boost::mutex> lock(my_mutex);
        if(my_queue.size()==0)
        {
            //如果队列中没有任务,则等待互斥锁
            cond.wait(lock);
        }
        //指向队列首部
        my_task task(my_queue.front());
        //出队列
        my_queue.pop();
        return task;      
    }
    int get_size()
    {
        return my_queue.size();
    }
};

#endif	

  1. #ifndef MY_THREAD_POOL_H   
  2. #define MY_THREAD_POOL_H   
  3. #include <boost/thread.hpp>   
  4. #include <boost/noncopyable.hpp>   
  5. #include <boost/function.hpp>   
  6. #include "My_Task_queue.h"   
  7. #include <iostream>   
  8. using namespace std;  
  9. class My_thread_pool:boost::noncopyable  
  10. {  
  11. private:  
  12.     //线程队列   
  13.     Task_queue my_queue;  
  14.     //线程组   
  15.     boost::thread_group my_thread_group;  
  16.     int thread_num;  
  17.     volatile bool is_run;  
  18.     void run()  
  19.     {  
  20.         while(is_run)  
  21.         {  
  22.             cout<<"run "<<endl;  
  23.             //一直处理线程池的任务   
  24.             my_task task=my_queue.get_task();  
  25.             task();  
  26.         }  
  27.     }  
  28. public:  
  29.     My_thread_pool(int num):thread_num(num),is_run(false)  
  30.     {  
  31.           
  32.     };  
  33.     ~My_thread_pool(){  
  34.       
  35.     };  
  36.     void init()  
  37.     {  
  38.         is_run=true;  
  39.         if(thread_num<=0)  
  40.         {  
  41.             return ;  
  42.         }  
  43.         for(int i=0;i<thread_num;++i)  
  44.         {  
  45.             //生成多个线程,绑定run函数,添加到线程组   
  46.             my_thread_group.add_thread(new boost::thread(boost::bind(&My_thread_pool::run,this)));  
  47.         }  
  48.     }  
  49.     //停止线程池   
  50.     void stop()  
  51.     {  
  52.         is_run=false;  
  53.     }  
  54.     //添加任务    
  55.     void post(const my_task &task)  
  56.     {  
  57.         my_queue.push_task(task);  
  58.     }  
  59.     void wait()  
  60.     {  
  61.         my_thread_group.join_all();  
  62.     }  
  63.                 
  64. };  
  65. #endif    
#ifndef MY_THREAD_POOL_H
#define	MY_THREAD_POOL_H
#include <boost/thread.hpp>
#include <boost/noncopyable.hpp>
#include <boost/function.hpp>
#include "My_Task_queue.h"
#include <iostream>
using namespace std;
class My_thread_pool:boost::noncopyable
{
private:
    //线程队列
    Task_queue my_queue;
    //线程组
    boost::thread_group my_thread_group;
    int thread_num;
    volatile bool is_run;
    void run()
    {
        while(is_run)
        {
            cout<<"run "<<endl;
            //一直处理线程池的任务
            my_task task=my_queue.get_task();
            task();
        }
    }
public:
    My_thread_pool(int num):thread_num(num),is_run(false)
    {
        
    };
    ~My_thread_pool(){
    
    };
    void init()
    {
        is_run=true;
        if(thread_num<=0)
        {
            return ;
        }
        for(int i=0;i<thread_num;++i)
        {
            //生成多个线程,绑定run函数,添加到线程组
            my_thread_group.add_thread(new boost::thread(boost::bind(&My_thread_pool::run,this)));
        }
    }
    //停止线程池
    void stop()
    {
        is_run=false;
    }
    //添加任务 
    void post(const my_task &task)
    {
        my_queue.push_task(task);
    }
    void wait()
    {
        my_thread_group.join_all();
    }
              
};
#endif	

  1. #include "My_Task_queue.h"   
  2. #include "My_thread_pool.h"   
  3. #include <iostream>   
  4. #include <cstdlib>   
  5. using namespace std;  
  6.   
  7. void print_task(int i)  
  8. {  
  9.     cout<<"I am Task "<<i<<" number:"<<endl;  
  10. }  
  11. //使用boost线程池  编译参数加上-lboost_thread   
  12. int main(int argc, char** argv) {  
  13.   
  14.     My_thread_pool tp(4);  
  15.     tp.init();  
  16.       
  17.     my_task task[4];  
  18.     for(int i=0;i<4;i++)  
  19.     {  
  20.         //生成线程任务,类型为函数指针 boost::function<void(void)>   
  21.         task[i]=boost::bind(print_task,i+1);  
  22.         //放到线程池中处理   
  23.         tp.post(task[i]);  
  24.     }  
  25.     //等待线程池处理完成!   
  26.    tp.wait();  
  27.       
  28.     return 0;  
  29. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值