简体   繁体   English

boost :: thread:如何启动所有线程,但一次最多只运行n个?

[英]boost::thread: How to start all threads, but have only up to n running at a time?

In the boost::thread library, is there any mechanism to control how many threads (at most) are running at a time? boost::thread库中,是否有任何机制来控制一次运行多少线程(最多)?

In my case, it would be most convenient to start N threads all at the same time (N may be hundreds or a few thousand): 在我的情况下,最方便的是同时启动N线程(N可能是几百或几千):

std::vector<boost::thread*> vec;
for (int i = 0; i < N; ++i) {
   vec.push_back(new boost::thread(my_fct));
}
// all are running, now wait for them to finish:
for (int i = 0; i < N; ++i) {
  vec[i]->join();
  delete vec[i];
}

But I want Boost to transparently set a maximum of, say, 4 threads running at a time. 但我希望Boost透明地设置最多,比如说,一次运行4个线程。 (I'm sharing an 8-core machine, so I'm not supposed to run more than 4 at a time.) (我正在共享一台8核机器,所以我不应该一次运行超过4台机器。)

Of course, I could take care of starting only 4 at a time myself, but the solution I'm asking about would be more transparent and most convenient. 当然,我自己可以一次只启动4个,但我要问的解决方案会更透明,更方便。

What you really want, it seems, would be to only ever have 4 threads, each of which would process many jobs . 看起来你真正想要的只是拥有4个线程,每个线程都会处理许多工作

One way to implement this would be to spawn as many threads as you like, and then for the run-loop of each threads to take tasks (typically function objects or pointers) from a thread-safe queue structure where you store everything that needs to be done. 实现这一点的一种方法是根据需要生成尽可能多的线程,然后为每个线程的运行循环从线程安全的队列结构中获取任务(通常是函数对象或指针),在这种结构中存储需要的所有内容。完成。

This way you avoid the overhead from creating lots of threads, and still maintain the same amount of concurrency. 这样可以避免创建大量线程的开销,并且仍然保持相同的并发量。

Don't think Boost.Thread has this built in but you can overlay Boost.Threadpool (not an official library) onto Boost.Thread, and that does allow you to control the thread count via SizePolicy . 不要认为Boost.Thread内置了这个,但你可以将Boost.Threadpool (不是官方库)覆盖到Boost.Thread上,这确实允许你通过SizePolicy控制线程数。

The default is a fixed-size pool which is what you want - specify the initial (and ongoing) thread count on the threadpool constructor. 默认值是一个固定大小的池这就是你想要的-指定的初始(和持续)的线程数threadpool的构造。

You could create a lock that you could only obtain n times. 你可以创建一个你只能获得n次的锁。 Then each thread should have to obtain the lock (blocking) before processing. 然后每个线程都必须在处理之前获得锁定(阻塞)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM