C++ 线程池
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3988128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Thread Pool
提问by Amir Rachum
What is a good open source implementation of a thread pool for C++ to use in production code (something like boost)?
在生产代码中使用 C++ 的线程池的好的开源实现是什么(比如 boost)?
Please provide either your own example code or a link to example code usage.
请提供您自己的示例代码或示例代码用法的链接。
采纳答案by Diego Sevilla
I think it is still not accepted into Boost, but a good staring point: threadpool. Some example of usage, from the web site:
我认为它仍然没有被 Boost 接受,但是一个很好的起点: threadpool。一些用法示例,来自网站:
#include "threadpool.hpp"
using namespace boost::threadpool;
// Some example tasks
void first_task()
{
...
}
void second_task()
{
...
}
void third_task()
{
...
}
void execute_with_threadpool()
{
// Create a thread pool.
pool tp(2);
// Add some tasks to the pool.
tp.schedule(&first_task);
tp.schedule(&second_task);
tp.schedule(&third_task);
// Leave this function and wait until all tasks are finished.
}
The argument "2" to the pool indicates the number of threads. In this case, the destruction of tp
waits for all threads to finish.
池的参数“2”表示线程数。在这种情况下,销毁tp
等待所有线程完成。
回答by Diego Sevilla
You might want to look at http://threadpool.sourceforge.net/
您可能想查看http://threadpool.sourceforge.net/
It is not hard to implement thread poolyourself using Boost.Thread. Depending on the task, you might want to use lock-freecontainer for the queue instead of one from Standard Template Library. For example, fifo
container from lock free
library.
使用Boost.Thread自己实现线程池并不难。根据任务,您可能希望对队列使用无锁容器,而不是标准模板库中的容器。例如,来自库的容器。fifo
lock free
Good luck!
祝你好运!
回答by tonicebrian
I've written a small example here. Basically what you need to do is to implement this piece of code:
我在这里写了一个小例子。基本上你需要做的是实现这段代码:
asio::io_service io_service;
boost::thread_group threads;
auto_ptr<asio::io_service::work> work(new asio::io_service::work(io_service));
// Spawn enough worker threads
int cores_number = boost::thread::hardware_concurrency();
for (std::size_t i = 0; i < cores_number; ++i){
threads.create_thread(boost::bind(&asio::io_service::run, &io_service));
}
// Post the tasks to the io_service
for(vector<string>::iterator it=tasks.begin();it!=tasks.end();it++){
io_service.dispatch(/* YOUR operator()() here */);
}
work.reset();
回答by Nim
I believe you can emulate a thread pool with an io_service in boost::asio. You can control the number of threads available to the io_service pool, and then you can "post" tasks to the io_service, which will get executed by one of the threads in the pool. Each such task has to be a functor (I believe).
我相信您可以在 boost::asio 中使用 io_service 模拟线程池。您可以控制 io_service 池可用的线程数,然后您可以将任务“发布”到 io_service,它将由池中的一个线程执行。每个这样的任务都必须是一个函子(我相信)。
I can't put an example here right now, but the asio documentation on io_service pools will outline how this can be done.
我现在不能在这里举一个例子,但是关于 io_service pools 的 asio 文档将概述如何做到这一点。
回答by heathbar
Here is a simple header-only task queue using a thread pool (built on Boost): taskqueue.hpp
这是一个使用线程池(建立在 Boost 上)的简单头任务队列:taskqueue.hpp
The TaskQueue project pageincludes a sample application demonstrating how to use it:
回答by Sumeet
回答by Steve Townsend
This librarybuilds on Boost.Thread. There is a short tutorialwith some example code. If this does not do what you want, you could use it as a baseline.
这个库建立在 Boost.Thread 之上。有一个包含一些示例代码的简短教程。如果这不能满足您的要求,您可以将其用作基线。
Make sure you are on a Boost version >= 1.37 if you go this route.
如果您走这条路线,请确保您使用的是 Boost 版本 >= 1.37。