C++ 使用 boost 创建线程池
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4084777/
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
Creating a thread pool using boost
提问by grich
Is it possible to create a thread pool using boost's thread? i was looking all over boost's libs and I couldn't find a thread pool manager (or something like that)... Is there a way to do it?
是否可以使用 boost 的线程创建线程池?我正在查看 boost 的所有库,但找不到线程池管理器(或类似的东西)......有没有办法做到这一点?
tnx!
天!
采纳答案by Armen Tsirunyan
There is an unofficial (yet) threadpoolin boost. But it's not a problem to implement one yourself especially if great genericity is not a primary goal. Idea: your threadpool can be parametrized with TaskType type and the number of workers. The TP must be given the handler function which takes TaskType. TP contains a queue of added tasks. The real thread function just takes a task from the queue and calls the passed handler. Something like that.
boost中有一个非官方(尚未)的线程池。但是自己实现一个不是问题,特别是如果很好的通用性不是主要目标。想法:您的线程池可以使用 TaskType 类型和工作人员数量进行参数化。必须为 TP 提供采用 TaskType 的处理函数。TP 包含一个添加任务的队列。真正的线程函数只是从队列中获取一个任务并调用传递的处理程序。类似的东西。
回答by Nim
I know an answer has been accepted, if you need this right now, and you can't be bothered to write your own thread pool, you could try using boost asio io_service with a concurrency hint (i.e. how many threads it should run) and then post() stuff to this io_service... just an idea..
我知道一个答案已被接受,如果您现在需要这个,并且您不想编写自己的线程池,您可以尝试使用带有并发提示的 boost asio io_service(即它应该运行多少线程)和然后 post() 东西到这个 io_service ......只是一个想法..