如何使升压:: thread_group执行并行线程的固定数量 [英] How to make boost::thread_group execute a fixed number of parallel threads

查看:270
本文介绍了如何使升压:: thread_group执行并行线程的固定数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是code创建一个thread_group和并行执行的所有线程:

This is the code to create a thread_group and execute all threads in parallel:

boost::thread_group group;
for (int i = 0; i < 15; ++i)
    group.create_thread(aFunctionToExecute);
group.join_all();

这code将同时执行所有线程。我想要做的就是执行他们所有,但4最大并行。当在终止时,另一个被执行,直到没有更多的要执行

This code will execute all threads at once. What I want to do is to execute them all but 4 maximum in parallel. When on is terminated, another one is executed until there are no more to execute.

推荐答案

另外,更高效的解决方案是让每个线程回调主线程,当他们完成,主线程的处理函数可以启动一个新的线程每一次。这prevents到timed_join重复呼叫,直到回调触发主线程不会做任何事情。

Another, more efficient solution would be to have each thread callback to the primary thread when they are finished, and the handler on the primary thread could launch a new thread each time. This prevents the repetitive calls to timed_join, as the primary thread won't do anything until the callback is triggered.

这篇关于如何使升压:: thread_group执行并行线程的固定数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆