本文首发于个人博客https://kezunlin.me/post/f241bd30/,欢迎阅读!

boost thread pool example

Guide

boost thread pool example with cpp code

code example

#include <iostream>    //std::cout std::endl
#include <thread> //std::thread
#include <future> //std::future std::promise
#include <utility> //std::ref
#include <chrono> //std::chrono::seconds #include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/asio.hpp> class ThreadPool {
public:
explicit ThreadPool(size_t size) : work_(io_service_) {
for (size_t i = 0; i < size; ++i) {
workers_.create_thread(
boost::bind(&boost::asio::io_service::run, &io_service_));
}
} ~ThreadPool() {
std::cout << "~ThreadPool" << std::endl;
io_service_.stop(); // stop before join_all
workers_.join_all();
} // Add new work item to the pool.
template<class F>
void Enqueue(F f) {
io_service_.post(f);
//sync, return immediately
} private:
boost::thread_group workers_;
boost::asio::io_service io_service_;
boost::asio::io_service::work work_;
}; boost::mutex io_mutex; void count(int id)
{
for (int i = 0; i < 10; i++)
{
boost::mutex::scoped_lock lock(io_mutex);
std::cout << id << ":" << i << std::endl;
}
} void test_thread()
{
boost::thread thrd1(boost::bind(&count, 1));
boost::thread thrd2(boost::bind(&count, 2));
thrd1.join();
thrd2.join();
} void print(int i)
{
boost::mutex::scoped_lock lock(io_mutex);
std::cout << "print() #" << boost::this_thread::get_id() << std::endl;
std::cout << "hello " << i << std::endl;
boost::this_thread::sleep(boost::posix_time::seconds(1));
std::cout << "world " << i << std::endl;
} void test_thread_pool()
{
// Create a thread pool of 4 worker threads.
ThreadPool pool(4); // Queue a bunch of work items.
for (int i = 0; i < 8; ++i) {
pool.Enqueue(boost::bind(&print, i));
}
} void do_task(std::promise<int> &promiseObj) {
boost::mutex::scoped_lock lock(io_mutex);
std::cout << "Inside thread: " << std::this_thread::get_id() << std::endl;
std::cout << "Inside thread: sleep 2 seconds... " << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2));
promiseObj.set_value(35);
} void test_future()
{
std::promise<int> promiseObj;
std::future<int> futureObj = promiseObj.get_future();
std::cout << "create thread..." << std::endl;
std::thread th(do_task, std::ref(promiseObj)); std::cout << "futureObj.get() block main thread." << std::endl;
std::cout << futureObj.get() << std::endl; th.join();
std::cout << "after join" << std::endl;
} /*
std::bind
bind预先绑定的参数需要传具体的变量或值进去,对于预先绑定的参数,是pass-by-value的;[使用std::ref()可以pass by reference]
对于不事先绑定的参数,需要传std::placeholders进去,从_1开始,依次递增。placeholder是pass-by-reference的;
*/ int main(int argc, char* argv[])
{
//test_thread();
//test_thread_pool();
test_future();
return 0;
}

Reference

History

  • 20180523: created.

Copyright

使用boost实现线程池thread pool | boost thread pool example的更多相关文章

  1. boost的线程池和内存池 智能指针

    内存池为boost自带的 #include <boost/pool/pool.hpp> 或者另外一个开源的库: nedmalloc 一个高效率的库 线程池需要下载另外一个开源库 http: ...

  2. Android 线程池系列教程(2)Thread,Runnable是基类及如何写Run方法

    Specifying the Code to Run on a Thread 上一课   下一课 1.This lesson teaches you to Define a Class that Im ...

  3. [图解tensorflow源码] 线程池模块分析 (CPU thread pool device)

  4. 搜集C++实现的线程池

    现在很多语言都内置了线程池实现,但C++中却没有.本文列举一些C++实现的线程池工具. Boost.Threadpool 项目首页:http://threadpool.sourceforge.net ...

  5. (原创)C++半同步半异步线程池2

    (原创)C++半同步半异步线程池 c++11 boost技术交流群:296561497,欢迎大家来交流技术. 线程池可以高效的处理任务,线程池中开启多个线程,等待同步队列中的任务到来,任务到来多个线程 ...

  6. 二 Java利用等待/通知机制实现一个线程池

    接着上一篇博客的 一Java线程的等待/通知模型 ,没有看过的建议先看一下.下面我们用等待通知机制来实现一个线程池 线程的任务就以打印一行文本来模拟耗时的任务.主要代码如下: 1  定义一个任务的接口 ...

  7. java笔记--使用线程池优化多线程编程

    使用线程池优化多线程编程 认识线程池 在Java中,所有的对象都是需要通过new操作符来创建的,如果创建大量短生命周期的对象,将会使得整个程序的性能非常的低下.这种时候就需要用到了池的技术,比如数据库 ...

  8. 线程池——JAVA并发编程指南

    TPS00-J. 用线程池实现应用在流量暴涨时优雅降级 很多程序都要解决这样一个问题——处理一系列外来的请求.Thread- Per-Message这种设计模式是最简单的并发策略了,它为每一个请求创建 ...

  9. 【MINA】OrderedThreadPoolExecutor和UnorderedThreadPoolExecutor的事件监听线程池的选择

    mina中有两个线程池概念 1.处理监听建立连接的线程池  2.处理读写事件的线程池 本文中主要探讨读写事件的线程池的选择 这两种都经过实际项目的使用和检测,说说优缺点 早期的项目是用Unordere ...

随机推荐

  1. 算法问题实战策略 DICTIONARY

    地址 https://algospot.com/judge/problem/read/DICTIONARY 解法 构造一个26字母的有向图 判断无回路后 就可以输出判断出来的字符序了 比较各个字母的先 ...

  2. java集合第一节,List简单介绍

    Java中List集合的常用方法   List接口是继承Collection接口,所以Collection集合中有的方法,List集合也继承过来. package 集合; import java.ut ...

  3. SpringBoot 遇到 No identifier specified for entity

    No identifier specified for entity 从字面上不难看出来是没有设置主键 因为没有为标注为@Entity的实体类注明主键 import lombok.Data; impo ...

  4. SQL查询选修了所有课程的学生姓名

    select sname from student where not exists (select * from course where not exists   (select * from s ...

  5. ThreadLocal小试牛刀

    ThreadLocal中保存的数据只能被当前线程私有,不被其它线程可见 证明 声明一个全局的变量threadLocal,初始值为1,通过3个线程对其进行访问修改设置,理论上threadLocal的最终 ...

  6. swift 实现 iOS摇一摇

    本博客包含了如何实现iOS摇一摇全步骤,包括了完整的代码. 先附上demo地址https://github.com/Liuyubao/LYBShake ,支持swift3.0+. 一.导包 项目主要使 ...

  7. 用GitLab Runner自动部署GitBook并不难

    相信很多程序员喜欢用 GitBook 来写电子书.教程或者博客,看了不少文章,貌似都缺少说明如何将 GitBook 部署到版本库,并自动在服务器上 build,然后将生成的静态网站部署到云服务器上. ...

  8. 读书笔记-《Maven实战》-2018/5/3

    5.7依赖调解 1.当一个项目有以下依赖关系的时候:A->B->C->X(1.0).A->D->X(2.0),X作为A的传递依赖而拥有两个版本,Maven为了解决以上问题 ...

  9. lucas定理(模板题题解)

    题目很简单,很暴力,就是组合数,没有其他的. 但是直接暴力会炸wow 我们可以利用Lucas定理来分解字问题. Lucas定理:C(n,m)(mod p)=C(n%p,m%p)*C(n/p,m/p)( ...

  10. 零基础Linux入门学习方法--如何做好笔记及长效知识复习记忆

    “工欲善其事必先利其器”. 此次学习的0基础教材为刘遄(Liu Chuán)老师的<Linux就该这么学>.学习目的是通过RHCE认证.有关RHCE认证介绍会在认识Linux及红帽认证中记 ...