xl_blocking_queue.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#ifndef SRC_COMMON_BLOCKING_QUEUE_H_
#define SRC_COMMON_BLOCKING_QUEUE_H_
#include <boost/thread.hpp>
#include <boost/noncopyable.hpp>
#include <queue>
 
template<typename T>
class xl_blocking_queue
  :boost::noncopyable
{
public:
  xl_blocking_queue()
    :mutex_(), queue_(), cond_()
  {
   
  }
  ~xl_blocking_queue(){}
 
  void put(const T& func)
  {
    boost::unique_lock<boost::mutex> lock(mutex_);
    queue_.push(func);
    cond_.notify_all();
  }
 
  T get()
  {
    boost::unique_lock<boost::mutex> lock(mutex_);
    if (queue_.size() == 0)
    {
      cond_.wait(lock);
    }
    T front(queue_.front());
    queue_.pop();
    return front;
  }
 
  unsigned size()
  {
    return queue_.size();
  }
 
  void notify_all()
  {
    cond_.notify_all();
  }
private:
  std::queue<T> queue_;
  boost::condition_variable_any cond_;
  boost::mutex mutex_;
};
 
 
#endif

xl_thread_pool.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<strong>#ifndef SRC_COMMON_THREAD_POOL_H_
#define SRC_COMMON_THREAD_POOL_H_
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/noncopyable.hpp>
 
#include <vector>
#include "xl_blocking_queue.h"
 
typedef boost::function<void (void)> thread_do_func;
 
class xl_thread_pool
  :boost::noncopyable
{
public:
  xl_thread_pool(int thread_num)
    :num_(thread_num), run_(false)
  {
     
  }
  ~xl_thread_pool()
  {
    if (run_)
    {
      stop();
    }
  }
 
  void start()
  {
    if (num_ <= 0) return;
    int i = 0;
    run_ = true;
    for(i=0;i<num_;i++)
    {
      boost::shared_ptr<boost::thread> thread(new boost::thread(boost::BOOST_BIND(&xl_thread_pool::run, this)));
      thread_arr_.push_back(thread);
    }
  }
 
  void stop()
  {
    run_ = false;
    queue_.notify_all();
  }
 
  void post(const thread_do_func& task)
  {
    if (thread_arr_.size() == 0)
    {
      task();
    }
    else
    {
      queue_.put(task);
    }   
  }
private:
  xl_blocking_queue<thread_do_func> queue_;
  std::vector<boost::shared_ptr<boost::thread> > thread_arr_;
  int num_;
  bool run_;
 
  void run()
  {
    while(run_)
    {
      thread_do_func task = queue_.get();
      task();
    }
  }
};
 
 
#endif

一个C++基于boost简单实现的线程池的更多相关文章

  1. 基于队列queue实现的线程池

    本文通过文章同步功能推送至博客园,显示排版可能会有所错误,请见谅! 写在前文:在Python中给多进程提供了进程池类,对于线程,Python2并没有直接提供线程池类(Python3中提供了线程池功能) ...

  2. 一个简单的python线程池框架

    初学python,实现了一个简单的线程池框架,线程池中除Wokers(工作线程)外,还单独创建了一个日志线程,用于日志的输出.线程间采用Queue方式进行通信. 代码如下:(不足之处,还请高手指正) ...

  3. 一个简单的linux线程池(转-wangchenxicool)

    线程池:简单地说,线程池 就是预先创建好一批线程,方便.快速地处理收到的业务.比起传统的到来一个任务,即时创建一个线程来处理,节省了线程的创建和回收的开销,响应更快,效率更高. 在linux中,使用的 ...

  4. 自定义简单版本python线程池

    python未提供线程池模块,在python3上用threading和queue模块自定义简单线程池,代码如下: #用threading queue 做线程池 import queue import ...

  5. concurrent.futures模块简单介绍(线程池,进程池)

    一.基类Executor Executor类是ThreadPoolExecutor 和ProcessPoolExecutor 的基类.它为我们提供了如下方法: submit(fn, *args, ** ...

  6. Linux杂谈: 实现一种简单实用的线程池(C语言)

    基本功能 1. 实现一个线程的队列,队列中的线程启动后不再释放: 2. 没有任务执行时,线程处于pending状态,等待唤醒,不占cpu: 3. 当有任务需要执行时,从线程队列中取出一个线程执行任务: ...

  7. 简单实现java线程池

    使用多线程以及线程池的意义无需多说,要想掌握线程池,最好的方法还是自己手动去实现. 一.实现思路      (网络盗图) 二.实现代码 1.线程池类 package com.ty.thread; im ...

  8. 基于C++11实现的线程池

    1.C++11中引入了lambada表达式,很好的支持异步编程 2.C++11中引入了std::thread,可以很方便的构建线程,更方便的可移植特性 3.C++11中引入了std::mutex,可以 ...

  9. 基于Linux/C++简单线程池的实现

    我们知道Java语言对于多线程的支持十分丰富,JDK本身提供了很多性能优良的库,包括ThreadPoolExecutor和ScheduleThreadPoolExecutor等.C++11中的STL也 ...

随机推荐

  1. Android v2.0 基本概念 - 浅谈

    目录 Android框架 Linux Kernel 系统运行库 Libraries Android Runtime Application Framework Application Android框 ...

  2. String or binary data would be truncated

    在使用Typed Dataset进行数据的插入时,会报这样的错:String or binary data would be truncated. 我碰到的原因是 数据库中字段的长度过段,插入时内容被 ...

  3. spark 操作hbase

    HBase经过七年发展,终于在今年2月底,发布了 1.0.0 版本.这个版本提供了一些让人激动的功能,并且,在不牺牲稳定性的前提下,引入了新的API.虽然 1.0.0 兼容旧版本的 API,不过还是应 ...

  4. 使用WebFrom来模拟一些MVC的MODEL与View的数据交互功能

    MVC中有一点非常闪瞎人眼的功能就是,可以根据Model与View视图来直接将页面也数据模型进行绑定,那么我们在想客户端发送页面时不需要进行各种控件赋值,不需要操心怎么渲染,当客户提交表单给服务器时也 ...

  5. react相关

    react 简单入门 ant 蚂蚁金服react组件 redux 阮一峰入门react material-ui组件库 webpack入门 http://www.jianshu.com/p/42e115 ...

  6. assert实现

    测试网站在国内国外的访问速度 关于C的右左法则 assert宏的实现(一道笔试题) 2010-11-09 13:05:48|  分类: c |  标签: |举报 |字号大中小 订阅     asser ...

  7. windows核心编程-信号量(semaphore)

    线程同步的方式主要有:临界区.互斥区.事件.信号量四种方式. 前边讲过了互斥器线程同步-----windows核心编程-互斥器(Mutexes),这章我来介绍一下信号量(semaphore)线程同步. ...

  8. jq事件绑定

    有些时候我们在页面中会动态的添加一下dom结构,当我们想要给这些结点添加事件时需要在此节点绑定一系列的操作. <a href="#" onclick="addBtn ...

  9. HTML5 canvas图像绘制方法与像素操作属性和方法

    图像绘制方法 drawImage()        向画布上绘制图像.画布或视频 像素操作属性和方法 width                                返回 ImageData ...

  10. 文字列表无缝向上滚动JavaScript代码

    <!DOCTYPE html> <html> <head> <meta charset=utf-> <title>文字列表无缝向上滚动Jav ...