使用boost实现线程池thread pool | boost thread pool example
本文首发于个人博客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
- Post author: kezunlin
- Post link: https://kezunlin.me/post/f241bd30/
- Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.
使用boost实现线程池thread pool | boost thread pool example的更多相关文章
- boost的线程池和内存池 智能指针
内存池为boost自带的 #include <boost/pool/pool.hpp> 或者另外一个开源的库: nedmalloc 一个高效率的库 线程池需要下载另外一个开源库 http: ...
- Android 线程池系列教程(2)Thread,Runnable是基类及如何写Run方法
Specifying the Code to Run on a Thread 上一课 下一课 1.This lesson teaches you to Define a Class that Im ...
- [图解tensorflow源码] 线程池模块分析 (CPU thread pool device)
- 搜集C++实现的线程池
现在很多语言都内置了线程池实现,但C++中却没有.本文列举一些C++实现的线程池工具. Boost.Threadpool 项目首页:http://threadpool.sourceforge.net ...
- (原创)C++半同步半异步线程池2
(原创)C++半同步半异步线程池 c++11 boost技术交流群:296561497,欢迎大家来交流技术. 线程池可以高效的处理任务,线程池中开启多个线程,等待同步队列中的任务到来,任务到来多个线程 ...
- 二 Java利用等待/通知机制实现一个线程池
接着上一篇博客的 一Java线程的等待/通知模型 ,没有看过的建议先看一下.下面我们用等待通知机制来实现一个线程池 线程的任务就以打印一行文本来模拟耗时的任务.主要代码如下: 1 定义一个任务的接口 ...
- java笔记--使用线程池优化多线程编程
使用线程池优化多线程编程 认识线程池 在Java中,所有的对象都是需要通过new操作符来创建的,如果创建大量短生命周期的对象,将会使得整个程序的性能非常的低下.这种时候就需要用到了池的技术,比如数据库 ...
- 线程池——JAVA并发编程指南
TPS00-J. 用线程池实现应用在流量暴涨时优雅降级 很多程序都要解决这样一个问题——处理一系列外来的请求.Thread- Per-Message这种设计模式是最简单的并发策略了,它为每一个请求创建 ...
- 【MINA】OrderedThreadPoolExecutor和UnorderedThreadPoolExecutor的事件监听线程池的选择
mina中有两个线程池概念 1.处理监听建立连接的线程池 2.处理读写事件的线程池 本文中主要探讨读写事件的线程池的选择 这两种都经过实际项目的使用和检测,说说优缺点 早期的项目是用Unordere ...
随机推荐
- C#发送电子邮件(SMTP)及outlook.com账号之概要
这是关于c#发送电子邮件(SMTP)的技术笔记,以”简报“形式呈现. 因为最后成功通过outlook.com发送了邮件,所以,我觉得还是有必要 记录一下其中的要点. 一.技术核心 .net Frame ...
- Git & Github 使用教程【1】入门篇
Github教程 1-1 版本管理工具简介 主要作用:备份文件.记录历史.回到过去.多端共享.独挡一面.团队协作 2-1 版本管理工具发展历史 3-1 Git下载和安装[略] 3-2 linux下安装 ...
- 图论-最小生成树<Kruskal>
昨天: 图论-最小生成树<Dijkstra,Floyd> 以上是昨天的Blog,有需要者请先阅读完以上再阅读今天的Blog. 可能今天的有点乱,好好理理,认真看完相信你会懂得 然而,文中提 ...
- jquery 往上滚动的时显示,上下滚动执行的js
$(document).on("mousewheel DOMMouseScroll", function (e) { var delta = (e.originalEvent.wh ...
- 使用maven替换项目依赖中的字节码
问题描述 我们偶尔会发现一些开源项目的问题,或者出于其他原因,想在某个dependency的代码中加几行或者删除几行来达到目的. 我这里遇到一个dubbo 2.7.3和open feign冲突的问题 ...
- pycharm的使用(day03复习整理)
pycharm的使用 file --> settings --> editor -->general --> change font size .... file --> ...
- Java基础(41)AbstractList类
AbstractList类的子类有AbstractSequentialList(其子类是LinkedList)和ArrayList 1.LinkedList 定义 public class Linke ...
- 在Eclipse中开发MapReduce程序
一.Eclipse的安装与设置 1.在Eclipse官网上下载eclipse-jee-oxygen-3a-linux-gtk-x86_64.tar.gz文件并将其拷贝到/home/jun/Resour ...
- NOIP模拟测试6
看题目就知道这是一个悲伤的故事... 但还有更悲伤的 考崩了,难以描述. T1把数据范围看成2^12,我TM也是够了... T2思路接近正解,但不知道想了个神魔东西跑了N遍dijstra T3最狗了, ...
- P2579 [ZJOI2005]沼泽鳄鱼(邻接矩阵,快速幂)
题目简洁明了(一点都不好伐) 照例,化简题目 给一张图,每一个时间点有一些点不能走,(有周期性),求从起点第k秒恰好在终点的方案数,可重复,不可停留. 额dp实锤 于是就被打脸了.... 有一种东西叫 ...