免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3997 | 回复: 6
打印 上一主题 下一主题

boost_thread库求教 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-07 20:52 |只看该作者 |倒序浏览
小弟最近在研究boost thread库
下面是自己写的1个基本的多线程处理事务的模型 跑是基本没问题 但是不懂效率上来讲 合理么
先谢谢各位了
  1. #include <iostream>
  2. #include <boost/thread.hpp>
  3. #include <boost/bind.hpp>
  4. #include <list>

  5. using namespace std;

  6. boost::mutex mutex;

  7. class ThreadMgr
  8. {
  9. public:

  10.     ThreadMgr(int i)
  11.         : m_work_flag(true),m_thread_num(i)
  12.     {
  13.         m_works.clear();
  14.         Start();
  15.     }

  16.     ~ThreadMgr()
  17.     {
  18.         m_thread_list.join_all();
  19.     }

  20.     void Stop()
  21.     {
  22.         m_work_flag = false;
  23.     }

  24.     void AddWork(int i)
  25.     {
  26.         m_works.push_back(i);
  27.     }

  28. private:

  29.     void Start()
  30.     {
  31.         for (int i = 0; i < m_thread_num; ++i)
  32.         {
  33.             boost::thread* work = new boost::thread(boost::bind(&ThreadMgr::CallThread, this));
  34.             m_thread_list.add_thread(work);
  35.         }
  36.     }

  37.     void CallThread()
  38.     {
  39.         while (m_work_flag)
  40.         {
  41.             int i = 0;
  42.             // 这里对m_work_flag的判断是为了在关闭按钮触发后及时停止后台的线程
  43.             while(m_work_flag && GetWork(i))
  44.             {
  45.                 boost::unique_lock<boost::mutex> lock(mutex);
  46.                 cout << "thread[" << boost::this_thread::get_id() << "] is doing work[" << i << "]." << endl;
  47.             }
  48.         }

  49.     }

  50.     bool GetWork(int& i)
  51.     {
  52.         boost::unique_lock<boost::mutex> lock(mutex);
  53.         if (m_works.empty())
  54.         {
  55.             return false;
  56.         }

  57.         i = m_works.front();
  58.         m_works.pop_front();
  59.         return true;
  60.     }

  61.     boost::thread_group    m_thread_list;
  62.     list<int> m_works;
  63.     bool m_work_flag;
  64.     int m_thread_num;
  65. };

  66. int main()
  67. {
  68.     ThreadMgr* tm = new ThreadMgr(4);

  69.     for (int i = 1; i < 10; ++i)
  70.     {
  71.         tm->AddWork(i);
  72.     }

  73.     for (int i = 10; i < 20; ++i)
  74.     {
  75.         tm->AddWork(i);
  76.     }

  77.     tm->Stop();    // 关闭按钮之类的触发

  78.     delete tm;

  79.     return 0;
  80. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-11-07 23:11 |只看该作者
各位大侠帮看看呀

论坛徽章:
0
3 [报告]
发表于 2011-11-08 02:15 |只看该作者
要讲求效率的话,可以考虑动态设定线程数量;大致来说,我认为,在有 n 个 cpu 的情形下,可以考虑开 n-1 个线程处理数据,同时主程序也处理一部分,最后合并。

论坛徽章:
0
4 [报告]
发表于 2011-11-08 10:05 |只看该作者
接话题咨询一下:
boost thread库比较POSIX pthread有啥区别和优势?

论坛徽章:
0
5 [报告]
发表于 2011-11-08 11:13 |只看该作者
回复 4# samzc2010


    个人觉得是移植性 还有易用性 很恰到好处的利用了boost的bind和function

论坛徽章:
0
6 [报告]
发表于 2011-11-08 11:15 |只看该作者
发现在57和58之间要加入1句
boost::this_thread::sleep(boost::posix_time::milliseconds(1));

在任务列表为空的时候交出cpu

论坛徽章:
0
7 [报告]
发表于 2011-11-08 11:16 |只看该作者
发现在57和58之间要加入1句
boost::this_thread::sleep(boost::posix_time::milliseconds(1));

在任务列表为空的时候交出cpu
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP