简体   繁体   English

在 C++ Linux 中限制或限制正在运行的线程

[英]Limit or throttle running threads in C++ Linux

I am using Pagmo, a C+ API for my optimization problem.我正在使用 Pagmo,一个 C+ API 来解决我的优化问题。 Pagmo launches a new thread when a new optimization is launched, via invocation of island.evolve().当启动新的优化时,Pagmo 通过调用 island.evolve() 启动一个新线程。 MY point is that I don't have fine-grained control here over the type of thread that's launched "under the hood" of Pagmo.我的观点是,我在这里对 Pagmo“幕后”启动的线程类型没有细粒度的控制。 I can query Pagmo threads on their status - as to whether they've completed their run.我可以查询 Pagmo 线程的状态——关于它们是否已完成运行。 I have a machine with 28 physical cores and I'm thinking that the optimum number of my running threads would be on the order of 28. Right now, my code just dumps the whole lot of threads onto the machine and that's substantially more than the number of cores - and hence, likely very inefficient.我有一台有 28 个物理内核的机器,我认为我正在运行的线程的最佳数量应该是 28 个。现在,我的代码只是将所有线程转储到机器上,这远远超过了核心数量 - 因此,效率可能非常低。 I'm thinking of using a std::counting_semaphore (C++ 20) and setting the counter to 28. each time I launch a thread in Pagmo, I would decrement the semaphore counter.我正在考虑使用 std::counting_semaphore (C++ 20) 并将计数器设置为 28。每次我在 Pagmo 中启动一个线程时,我都会减少信号量计数器。 When it hits 0, the remaining threads would block and wait in the queue until the counter was incremented.当它达到 0 时,其余线程将阻塞并在队列中等待,直到计数器递增。 I'm thinking I could run a loop which queried the Pagmo threads as to their statuses and increment the std::counting_semaphore's counter each time a thread went idle (meaning its task was completed).我想我可以运行一个循环来查询 Pagmo 线程的状态,并在每次线程空闲时增加 std::counting_semaphore 的计数器(意味着它的任务已完成)。 Of course, the Pagmo threads are ultimately joined.当然,Pagmo 线程最终是加入的。 Each time the counter goes above 0, a new thread is allowed to start - so I believe.每次计数器超过 0 时,都允许启动一个新线程 - 我相信。 My questions are:我的问题是:

  1. is the the best practice as to how to limit/throttle the number of running threads using modern C++?关于如何使用现代 C++ 限制/限制正在运行的线程数的最佳实践是什么?
  2. Is there a better way I haven't thought of?有没有我没有想到的更好的方法?
  3. Is there a way to query Linux in real time, as to the number of running threads?有没有办法实时查询Linux,运行线程数? Thanks in advance!提前致谢! Phil菲尔

I had tried a simple loop to launch and throttle theads creation but it didn't prove to work well and threads were launched too quickly.我曾尝试过一个简单的循环来启动和限制广告的创建,但事实证明效果不佳,线程启动得太快。

First of all, your post could use some editing and even perhaps provide a code snippet that would help us understand the problem more.首先,您的帖子可以进行一些编辑,甚至可以提供一个代码片段来帮助我们更好地理解问题。 Right now I'm only going through the documentation based on a wild guess of what you are doing there.现在我只是根据对你在那里做什么的疯狂猜测来浏览文档。

I've quickly checked what Pagmo is about and I would at first advise to be careful when limiting any library that is designed for parallel computation, from outside of the library.我已经快速检查了Pagmo是关于什么的,我首先建议在从库外部限制任何为并行计算而设计的库时要小心。

I will try to answer your questions:我会尽量回答你的问题:

  1. I do not think that this is the best way to throttle threads created by an external library我认为这不是限制外部库创建的线程的最佳方法
  2. Yes, first of all I've checked the Pagmo API documentation and if I understand you correctly you are using an island class - based on what they state in their documentation the default class that inherits island and is constructed by the default ctor is thread_island (at least on non-POSIX systems - which may not be your case).是的,首先我检查了Pagmo API 文档,如果我理解正确的话,你正在使用一个island类——根据他们在文档中的声明,继承island并由默认 ctor 构造的默认类是thread_island (至少在非 POSIX 系统上——这可能不是你的情况)。 However thread_island can be constructed via the thread_island(bool use_pool) ctor which indicates that you can specify to these island classes a common thread pool which they can use.然而, thread_island可以通过thread_island(bool use_pool) ctor构造,这表明您可以为这些island类指定一个它们可以使用的公共线程池。 And if it is done for non-POSIX systems, it is most likely done for POSIX systems as well.如果它是针对非 POSIX 系统完成的,那么它很可能也适用于 POSIX 系统。 So if you wish to limit the number of threads I would do this via a thread pool.所以如果你想限制线程的数量,我会通过线程池来做到这一点。
  3. You can limit the maximum number of threads running on linux via /proc/sys/kernel/threads-max , you can also instruct a Linux system to treat some processes with less importance via niceness您可以通过/proc/sys/kernel/threads-max限制在 linux 上运行的最大线程数,您还可以通过niceness指示 Linux 系统处理一些不太重要的进程

Hope it helps!希望能帮助到你!

EDIT: As a foot note I will also mention that the documentation actually encourages the use of thread_island even on POSIX systems.编辑:作为脚注,我还要提到文档实际上鼓励使用thread_island即使在 POSIX 系统上也是如此。 See this link here 在此处查看此链接

EDIT2: In case that you must use fork_island due to their mentioned issues when thread_safety cannot be guaranteed. EDIT2:如果由于无法保证thread_safety时提到的问题而必须使用fork_island Then another option would be to limit available resources via setrlimit see this link right here - you are interested in setting RLIMIT_NPROC那么另一种选择是通过setrlimit限制可用资源,请在此处查看此链接- 您有兴趣设置RLIMIT_NPROC

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

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