如何在任何给定时间仅使用线程池中特定数量的线程

发布于 2024-08-24 17:13:56 字数 412 浏览 6 评论 0原文

我发现这个问题对于学习基础知识非常有用的线程池。

现在我的问题在于使用 ThreadPool 执行一系列“任务”,例如斐波那契类,但希望一次最多执行 n 个这些任务,或者基本上限制这些任务因为它们在线程池中执行最多 n 个线程,并在执行任务完成时生成新线程。

使用 for 循环、任务计数器和 WaitHandle::WaitAny() 是否可行?

或者更确切地说,这会是一个糟糕的实现,我应该尝试其他一些多线程方法吗?

更新:

重新标记为 c++-cli,而不是 c++。

I found this question that was very useful in learning the basics of the ThreadPool.

Now my questions lies in using the ThreadPool for a series of "tasks", like the Fibonacci class, but wanting to have at most n number of these tasks executing at any one time, or basically limiting these tasks as they execute in the ThreadPool to a maximum of n threads and spawning new ones as executing tasks are completed.

Is this doable using a for loop, a task counter, and then WaitHandle::WaitAny().

Or rather would this be a bad implementation and should I be trying some other multithreaded approach?

Update:

Retagged as c++-cli, not c++.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

橘香 2024-08-31 17:13:56

我想你会发现 信号量 是一个非常适合这个。

信号量将使您能够以良好的线程安全方式管理对资源(即线程池)的访问。

但请注意,线程池实际上是用于轻量级处理,而不是大量繁重的处理。如果您打算做一些真正计算密集型的事情,您可以考虑一些 PFX ( .NET 中的并行框架扩展?),或管理您自己的工作线程。

I think you'll find a semaphore is an excellent fit for this.

A semaphore will enable you to manage access to a resource (i.e. the threadpool) in a nice, thread safe manner.

Note, though, that the threadpool is really for lightweight processing--not a lot of heavy lifting. If you're planning to do some real computationally intense things, you might consider some of the PFX (parallel framework extenstions?) in .NET, or managing your own worker threads.

韶华倾负 2024-08-31 17:13:56

正如其他人指出的那样 SetMaxThreads 会做在 C# 中...

对于 C++:在 MFC 中没有 ThreadPool,但有几种可用的实现。有一个 Threadpool 的 posix 版本,然后是boost 友好的ThreadPool。大多数实现应该有一种方法来限制可以同时运行的线程数量,但您必须检查文档。

As others have pointed out SetMaxThreads will do it in C#...

For C++: in MFC there is no ThreadPool, but there are several implementations that are available out there. There is a posix version of a Threadpool and then there is the boost friendly ThreadPool. Most implementations should have a way to limit the number of threads that can run at the same time, but you'd have to check the documentation.

忘东忘西忘不掉你 2024-08-31 17:13:56

也许最简单的方法是将 ThreadPool 包装在您自己的类中。将作业提交到包装器并将它们存储在队列中。如果运行的“作业”数量少于所需数量,请向线程池提交一个。当您的每项作业完成后,向包装器发出信号表明它已完成,并且它可以将下一个作业放入队列中。

这样,您就可以限制对 ThreadPool 的使用,而不会影响任何其他代码。

我不确定这是否足够清楚?

Probably the easiest thing to do is to wrap the ThreadPool in a class of your own. Submit jobs to your wrapper and store them in a queue. If you have less than the desired number of "jobs" running, submit one to the ThreadPool. When each of your jobs is done, signal the wrapper that it's done, and it can queue the next job in your queue.

This way, you limit your usage of the ThreadPool, without impacting any other code.

I'm not sure if that's clear enough?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文