我可以对 C# System.Threading.ThreadPool 进行分区吗?

发布于 2024-10-17 15:19:39 字数 323 浏览 3 评论 0原文

我喜欢线程池。它让我的生活更美好。然而,我的爱可能已经悄然变成了一种我需要逃离的虐待关系,所以我需要我的兄弟们(大概还有姐妹们,尽管我还没有看到任何实际的证据)的一些建议。

我的基本问题是,我有几个不同的库,它们都以不协调的方式使用线程池,并且可能会耗尽线程。我希望有某种方法可以对 ThreadPool 进行分区,这样我就可以提供某个 1 类线程、另外 20 个线程、另外 5 个线程,依此类推。

我知道我可以编写自己的 ThreadPool 实现。我不想这么做,因为我很懒。那么,是否有一个简单的解决方案?

目前我只能使用 3.5 CLR。我知道很多事情在 4.0 中变得更容易。

I love ThreadPool. It makes my life better. However, my love may have quietly turned into an abusive relationship that I need to escape from, so I need some advice from my SO brothers (and presumably sisters, although I haven't seen any actual evidence of that yet).

My basic problem is that I have several different libraries that are all using the threadpool in an uncoordinated way, and running out of threads is a possibility. I was hoping there was some way I could partition the ThreadPool up so I could give a certain class 1 thread, another 20 threads, another 5 threads, and so on.

I know I could write my own ThreadPool implementation. I don't want to do that, because I'm lazy. So, is there a simple solution already out there?

Currently I'm constrained to using the 3.5 CLR. I know a lot of this stuff becomes easier in 4.0.

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

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

发布评论

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

评论(3

烂柯人 2024-10-24 15:19:39

不,不幸的是没有。这也是我对 .NET 线程池的困扰之一。即使在具有并行扩展的 .NET 4 中,它通常也基于单个系统线程池 - 您必须做一些工作来创建自己的单独线程池(并将其作为任务调度程序或类似的东西提供给任务)。

我怀疑微软对此进行了研究,并发现大多数客户都使用单个线程池 - 但我同意这里有更多的灵活性会很好......

Nope, unfortunately not. It's one of my bugbears with the .NET thread pool as well. Even in .NET 4 with Parallel Extensions, it's generally based on a single system thread pool - you'd have to do some work to create your own individual thread pools (and feed that into tasks as a task scheduler, or something similar).

I suspect MS has researched this and found that most customers are find with a single thread pool - but I agree it would be nice to have a bit more flexibility here...

捂风挽笑 2024-10-24 15:19:39

不要重写自己的ThreadPool。

为什么不使用适配器模式来实际实现您需要的逻辑?

Don't rewrite your own ThreadPool.

Why don't you just use an Adapter Pattern to actually implement just the logic you need?

若言繁花未落 2024-10-24 15:19:39

我根据 MSDN 文章的理解是,线程池适用于短生命周期的线程。线程池的整个想法是创建新的内核线程,这会产生大量开销,因此创建一个内核线程并让该线程处理一件事,然后再转到下一件事。

如果线程池中有长时间运行的线程,则需要将它们移至常规线程。

如果线程“用完”,那么它们运行的​​时间就太长了。

My understanding based on MSDN articles is that the thread pool is meant for short lived threads. The whole idea of the thread pool is creating new kernel threads has lots of overhead, so create one kernel thread and let that thread do work on one thing, then move to the next.

If you have long running threads in your thread pool, you need to move those to regular threads.

If you're "running out" of threads, then they're running too long.

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