找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 3665|回复: 0

用boost自制线程池

[复制链接]

307

主题

228

回帖

7343

积分

用户组: 真·技术宅

UID
2
精华
76
威望
291 点
宅币
5593 个
贡献
253 次
宅之契约
0 份
在线时间
948 小时
注册时间
2014-1-25
发表于 2015-3-3 15:04:40 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×
初学boost,看到asio库如此好用,就做了个简单的线程池

  1. // test.cpp : 定义控制台应用程序的入口点。
  2. //


  3. #include <iostream>
  4. #include <boost/asio.hpp>
  5. #include <boost/bind.hpp>
  6. #include <boost/thread.hpp>
  7. #include <boost/thread/detail/thread_group.hpp>
  8. #include <boost/scoped_ptr.hpp>
  9. #include <deque>
  10. using namespace std;

  11. std::deque<int> task;

  12. boost::mutex output;//执行函数用的资源锁


  13. //任务函数
  14. void func(int task)//taskindex任务数据索引,用于寻找指定的任务
  15. {
  16.         boost::mutex::scoped_lock mio(output);
  17.         cout << task << "th task run" << endl;
  18. #include <stdlib.h>
  19.         Sleep((rand() % 10) * 100);//增大随机性,看到交叉执行的情况
  20.         cout << task << "th task end" << endl;
  21. }

  22. template<typename TASKTYPE>
  23. class ThreadPool
  24. {
  25. public:
  26.         //用于将task里的任务分配各给threadnum个活动线程,分配时要进行线程同步
  27.         void wrapper(boost::asio::io_service* service)
  28.         {
  29.                 while (true)
  30.                 {
  31.                         mu.lock();
  32.                         if (task.empty())
  33.                         {
  34.                                 mu.unlock();
  35.                                 return;
  36.                         }
  37.                         service->post(boost::bind(func, task.front()));
  38.                         task.pop_front();
  39.                         mu.unlock();
  40.                         service->run();
  41.                         service->reset();
  42.                 }
  43.         }

  44.         ThreadPool(int _threadnum, std::deque<TASKTYPE>& _task) :threadnum(_threadnum), task(_task)
  45.         {
  46.                 ppio = new boost::asio::io_service*[threadnum];
  47.                 if (!ppio)
  48.                         throw "Error";
  49.                 for (int i = 0; i < threadnum; i++)
  50.                 {
  51.                         ppio[i] = new boost::asio::io_service;
  52.                         if (!ppio[i])
  53.                                 throw "Error";
  54.                 }
  55.         }

  56.         bool Exec()
  57.         {
  58.                 try
  59.                 {
  60.                         //建立线程组
  61.                         boost::thread_group tg;
  62.                         for (int i = 0; i < threadnum; i++)
  63.                         {
  64.                                 tg.create_thread(boost::bind(&ThreadPool::wrapper, this, ppio[i]));
  65.                         }
  66.                         //等待所有任务执行完毕
  67.                         tg.join_all();
  68.                 }
  69.                 catch (...)
  70.                 {
  71.                         //确保释放new
  72.                         return false;
  73.                 }
  74.                 return true;
  75.         }

  76.         virtual ~ThreadPool()
  77.         {
  78.                 for (int i = 0; i < threadnum; i++)
  79.                         delete ppio[i];
  80.                 delete[]ppio;
  81.         }
  82. private:
  83.         int threadnum;
  84.         boost::mutex mu;//分配任务用锁
  85.         std::deque<TASKTYPE>& task;//要分配的总任务
  86.         boost::asio::io_service** ppio;//耗费的线程资源      PS:如果Io_service不是noncopyable,我也不用这么费劲用指针做,连vector都不让用
  87. };

  88. void main()
  89. {
  90. //用法示例:
  91.         int tasknum = 1000;
  92.         for (int i = 0; i < tasknum; i++)
  93.         {
  94.                 task.push_back(i);//新增1000个任务
  95.         }
  96.         //使用3线程完成任务
  97.         ThreadPool<int> newtaskset(3, task);
  98.         newtaskset.Exec();
  99.         cin >> tasknum;//等待运行
  100. }
复制代码


有了这个就可以做我的dns多线程查询工具了!!!
回复

使用道具 举报

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-4-29 03:34 , Processed in 0.042163 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表