设置它的代码完成后,ThreadPool是否会重置最大线程数?我应该手动执行此操作吗? [英] Does the ThreadPool reset the maximum threads after the code that sets it finishes? Should I manually do this?

查看:46
本文介绍了设置它的代码完成后,ThreadPool是否会重置最大线程数?我应该手动执行此操作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一些(c#3.5)批处理代码,可以选择重写线程池maxthreads.它使用了一些不是线程安全的大量资源,因此需要为每个活动线程维护一个资源池"和一个项目.消耗用户的应用程序可以设置批处理处理器将使用的线程数的限制,该限制又调用 ThreadPool.SetMaxThreads().我这样做是为了测试手动设置的限制是否比将其保留为默认设置的平均性能更好.

I have written some (c# 3.5) batch processing code that can optionally override the thread pool maxthreads. It uses some pretty heavy resources that are not thread-safe, requiring a "resource pool" to be maintained with an item for each active thread. The consuming application can set a limit on how many threads the batch processor will use, which in turn calls ThreadPool.SetMaxThreads(). I'm doing this to test if manually setting a limit averages better performance than leaving it at the default setting.

我的问题是,每次使用批处理代码后,我是否应该将maxthreads重新设置为应用程序的先前值?一整天都会断断续续地调用它,可能有数千次.MaxThreads限制会一直持续到应用程序重新启动/将其设置为其他值之前,还是会自动重置?

My question is, should I reset the maxthreads back to the previous value for the application after each use of the batch processing code? It will be called intermittently throughout the day, possibly thousands of times. Will the MaxThreads limit persist forever until the application is restarted / it is set to a different value, or will it automatically reset?

尽管这不是我的测试工具真正关心的问题,但我想知道当一批完成在生产环境中使用的其他资源被处置后,是否应该更新代码以重置"线程池.只是将其覆盖有什么含义吗?

While this isn't really a concern in my test harness, I'm wondering if I should update the code to "reset" the threadpool when the other resources are disposed after a batch completes for use in a production environment. Are there any implications to just leaving it overridden?

最后,在一个.net应用程序中设置线程池是将它设置为对盒子上的所有应用程序还是仅针对该单个应用程序的上下文进行设置?

Finally, does setting the threadpool in one .net application set it for ALL applications on the box, or only for the context of that single app?

推荐答案

TL; DR:不要使用 SetMaxThreads .

TL;DR: Don't use SetMaxThreads.

此设置是按进程设置的:其他进程不受影响,但是整个应用程序都会受到影响!这被称为"针对局部问题的全球解决方案",被认为是一件坏事.

This setting is per-process: Other processes are not affected, but your entire application is! This is called "a global solution for a local problem" and is considered a bad thing.

您只希望应用程序的一部分受到此设置的影响,但是您正在影响整个应用程序.

You want only some part of your app to be affected by this setting but you are affecting the whole app.

例如,如果将max-threads设置为较低的值以限制并发性,则会使整个应用程序的其余部分都饿死,包括ASP.NET请求线程等.

For example if you set max-threads to a low value to limit concurrency you will starve out the rest of the entire application including ASP.NET request threads and such.

基于这个原因,我总是喜欢在本地解决此类问题.许多TPL原语(PLINQ,Parallel.*)允许您设置最大并行度.对于简单任务,可以使用具有固定线程数(ParallelExtras中提供的代码)的自定义任务计划程序.

For that reason I always prefer to solve such issues locally. Many TPL primitives (PLINQ, Parallel.*) allow you to set a max degree of parallelism. For simple tasks you can use a custom task scheduler which has a fixed number of threads (code available in the ParallelExtras).

这篇关于设置它的代码完成后,ThreadPool是否会重置最大线程数?我应该手动执行此操作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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