线程池的C++11简单实现,源代码来自Github上作者progschj,地址为:A simple C++11 Thread Pool implementation,具体博客可以参见Jakob’s Devlog,地址为:A Thread Pool with C++11

1、线程池的实现代码如下:

ThreadPool.h

#ifndef THREAD_POOL_H
#define THREAD_POOL_H #include <vector>
#include <queue>
#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
#include <functional>
#include <stdexcept> class ThreadPool {
public:
ThreadPool(size_t);
template<class F, class... Args>
auto enqueue(F&& f, Args&&... args)
-> std::future<typename std::result_of<F(Args...)>::type>;
~ThreadPool();
private:
// need to keep track of threads so we can join them
std::vector< std::thread > workers;
// the task queue
std::queue< std::function<void()> > tasks; // synchronization
std::mutex queue_mutex;
std::condition_variable condition;
bool stop;
}; // the constructor just launches some amount of workers
inline ThreadPool::ThreadPool(size_t threads)
: stop(false)
{
for(size_t i = 0;i<threads;++i)
workers.emplace_back(
[this]
{
for(;;)
{
std::function<void()> task; {
std::unique_lock<std::mutex> lock(this->queue_mutex);
this->condition.wait(lock,
[this]{ return this->stop || !this->tasks.empty(); });
if(this->stop && this->tasks.empty())
return;
task = std::move(this->tasks.front());
this->tasks.pop();
} task();
}
}
);
} // add new work item to the pool
template<class F, class... Args>
auto ThreadPool::enqueue(F&& f, Args&&... args)
-> std::future<typename std::result_of<F(Args...)>::type>
{
using return_type = typename std::result_of<F(Args...)>::type; auto task = std::make_shared< std::packaged_task<return_type()> >(
std::bind(std::forward<F>(f), std::forward<Args>(args)...)
); std::future<return_type> res = task->get_future();
{
std::unique_lock<std::mutex> lock(queue_mutex); // don't allow enqueueing after stopping the pool
if(stop)
throw std::runtime_error("enqueue on stopped ThreadPool"); tasks.emplace([task](){ (*task)(); });
}
condition.notify_one();
return res;
} // the destructor joins all threads
inline ThreadPool::~ThreadPool()
{
{
std::unique_lock<std::mutex> lock(queue_mutex);
stop = true;
}
condition.notify_all();
for(std::thread &worker: workers)
worker.join();
} #endif

2、基本的用法:

// create thread pool with 4 worker threads
ThreadPool pool(4); // enqueue and store future
auto result = pool.enqueue([](int answer) { return answer; }, 42); // get result from future
std::cout << result.get() << std::endl;

3、测试C++11线程池的例子程序如下:

example.cpp

#include <iostream>
#include <vector>
#include <chrono> #include "ThreadPool.h" int main()
{ ThreadPool pool(4);
std::vector< std::future<int> > results; for(int i = 0; i < 8; ++i) {
results.emplace_back(
pool.enqueue([i] {
std::cout << "hello " << i << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "world " << i << std::endl;
return i*i;
})
);
} for(auto && result: results)
std::cout << result.get() << ' ';
std::cout << std::endl; return 0;
}

编译运行example.cpp时需要添加C++11的支持,并且要加入pthread库的链接。

在Linux下的编译的命令为: g++ example.cpp -o example -std=c++11 -lpthread;如果在VS2017中使用,由于默认支持C++11,可以直接创建一个控制台程序,添加ThreadPool.h和example.cpp到项目,编译运行即可。

在Ubuntu18.10下测试如下:

havealex@havealex:~/GithubProjects/ThreadPool$ ls
COPYING example.cpp README.md ThreadPool.h
havealex@havealex:~/GithubProjects/ThreadPool$ g++ example.cpp -o example -std=c++11
/usr/bin/ld: /tmp/ccduRGse.o: in function `std::thread::thread<ThreadPool::ThreadPool(unsigned long)::{lambda()#1}, , void>(ThreadPool::ThreadPool(unsigned long)::{lambda()#1}&&)':
example.cpp:(.text._ZNSt6threadC2IZN10ThreadPoolC4EmEUlvE_JEvEEOT_DpOT0_[_ZNSt6threadC5IZN10ThreadPoolC4EmEUlvE_JEvEEOT_DpOT0_]+0x2f): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
havealex@havealex:~/GithubProjects/ThreadPool$ g++ example.cpp -o example -std=c++11 -lpthread
havealex@havealex:~/GithubProjects/ThreadPool$ ls\
> ^C
havealex@havealex:~/GithubProjects/ThreadPool$ ls
COPYING example example.cpp README.md ThreadPool.h
havealex@havealex:~/GithubProjects/ThreadPool$ ./example
hello 0
hello 1
hello 2
hello 3
world 1
hello 4
world 0
hello 5
0 1 world 2
hello 6
4 world 3
hello 7
9 world 5
world 4
16 25 world 6
36 world 7
49

可以看出,结果的输出是无序并行的。

简单的C++11线程池实现的更多相关文章

  1. 托管C++线程锁实现 c++11线程池

    托管C++线程锁实现   最近由于工作需要,开始写托管C++,由于C++11中的mutex,和future等类,托管C++不让调用(报错),所以自己实现了托管C++的线程锁. 该类可确保当一个线程位于 ...

  2. c++11线程池实现

    咳咳.c++11 增加了线程库,从此告别了标准库不支持并发的历史. 然而 c++ 对于多线程的支持还是比較低级,略微高级一点的使用方法都须要自己去实现,譬如线程池.信号量等. 线程池(thread p ...

  3. c++ 11 线程池---完全使用c++ 11新特性

    前言: 目前网上的c++线程池资源多是使用老版本或者使用系统接口实现,使用c++ 11新特性的不多,最近研究了一下,实现一个简单版本,可实现任意任意参数函数的调用以及获得返回值. 0 前置知识 首先介 ...

  4. c++11 线程池学习笔记 (一) 任务队列

    学习内容来自一下地址 http://www.cnblogs.com/qicosmos/p/4772486.html github https://github.com/qicosmos/cosmos ...

  5. C++11线程池的实现

    什么是线程池 处理大量并发任务,一个请求一个线程来处理请求任务,大量的线程创建和销毁将过多的消耗系统资源,还增加了线程上下文切换开销. 线程池通过在系统中预先创建一定数量的线程,当任务请求到来时从线程 ...

  6. Linux下简单的多线程编程--线程池的实现

    /* 写在前面的话: 今天刚“开原”,选择了一篇关于线程池的文件与大家分享,希望能对您学习有所帮助,也希望能与大家共同学习! 选择在这个特殊的时候注册并发文章也是有一些我个人特殊的意义的,看我的id( ...

  7. 简单明了的Java线程池

    线程池 线程池从功能上来看,就是一个任务管理器.在Java中,Executor接口是线程池的根接口,其中只包含一个方法: Executor void execute(Runnable command) ...

  8. 基于C++11线程池

    1.包装线程对象 class task : public std::tr1::enable_shared_from_this<task> { public: task():exit_(fa ...

  9. 简单RPC框架-业务线程池

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

随机推荐

  1. vue.js(16)--vue的组件

    注册一个全局组件 <div id="app"> <test></test> </div> <script> // 注册全 ...

  2. 采用pacemaker+corosync实现postgresql双机热备、高可用方案

    环境说明 参照上章已完成postgresql流复制配置,并关闭postgres服务. su - postgres pg_ctl -D /data/postgresql/data/ stop -m fa ...

  3. python特殊的类属性

    类C的特殊属性: C.__name__ 类C的名字 C.__doc__ 类C文档字符串 C.__bases__ 类C所有父类的元组 C.__dict__ 类C的属性 C.__module__ 类C所在 ...

  4. nginx的RPM包制作案例

    使用nginx-1.12.2版本的源码软件,生成对应的RPM包软件,具体如下: - 软件名称为nginx - 软件版本为1.12.2 - RPM软件包可以查询描述信息 - RPM软件包可以安装及卸载 ...

  5. vue-cli 2.0搭建vue脚手架步骤

    1.安装node 检测版本node -v 2.安装webpack npm install webpack -g 检测版本 webpack -v 3.安装vue-cli npm install vue- ...

  6. redis 并发测试安全测试代码

    package com.jd.ng.shiro.controller; import org.slf4j.Logger;import org.slf4j.LoggerFactory;import or ...

  7. django 使用ORM插入数据,提示Cannot assign "1": "B" must be a "Projectconfig" instance.

    这是因为使用了外键导致的, 如果使用了外键,先实例化外键查询,然后再插入的表里面放入实例化后的外键连接

  8. Mac新手入门:mac操作技巧

    面对全新的mac电脑,你是不是一脸的迷茫,一些原来windows上的基本操作在mac上都不知道从何入手了,下面小编就为大家整理了一些基本的操作.相信一定会方便你的学习和工作的. 如何压缩与解压缩 在M ...

  9. 【leetcode】394. Decode String

    题目如下: 解题思路:这种题目和四则运算,去括号的题目很类似.解法也差不多. 代码如下: class Solution(object): def decodeString(self, s): &quo ...

  10. python 全栈开发,Day54(关于DOM操作的相关案例,JS中的面向对象,定时器,BOM,client、offset、scroll系列)

    04-jQuery的属性操作 jquery的属性操作模块分为四个部分:html属性操作,dom属性操作,类样式操作和值操作 html属性操作:是对html文档中的属性进行读取,设置和移除操作.比如at ...