简体   繁体   English

将任务和线程池一起使用是否可以?

[英]Is it fine to use tasks and thread-pool together?

After reading how the thread pool and tasks work in this article I came up with this question - 读线程池和任务是如何工作后, 这个文章中,我提出了这个问题-

If I have a complex program in which some modules use tasks and some use thread pool , is it possible that there will be some scheduling problems due to the different uses? 如果我有一个复杂的程序,其中一些模块使用tasks ,一些使用thread pool ,是否可能由于不同的用途会有一些调度问题?

Task are often implemented using the thread pool (one can of course also have tasks using other types of schedulers that give different behavior, but this is the default). 任务通常使用线程池实现(当然也可以使用其他类型的调度程序来执行任务,这些调度程序提供不同的行为,但这是默认设置)。 In terms of the actual code being executed (assuming your tasks are representing delegates being run) there really isn't much difference. 就正在执行的实际代码而言(假设您的任务代表正在运行的代理),确实没有太大区别。

Tasks are simply creating a wrapper around that thread pool call to provide additional functionality when it comes to gather information about, and processing the results of, that asynchronous operation. 任务只是围绕该线程池调用创建一个包装器,以便在收集有关该异步操作的信息和处理该异步操作的结果时提供其他功能。 If you want to leverage that additional functionality then use tasks. 如果您想利用其他功能,请使用任务。 If you have no need to use it in some particular context, there's nothing wrong with using the thread pool directly. 如果您不需要在某些特定上下文中使用它,则直接使用线程池没有任何问题。

Mix the two, so long as you don't have trouble getting what you want out of the results of those operations, is not a problem at all. 混合两者,只要你没有从这些操作的结果中得到你想要的东西,根本不是问题。

No, there wouldn't be problems - you just would be inefficient in doing both. 不,不会有问题 - 你只会做两者效率低下。 use what is really needed and stick with the pattern. 使用真正需要的东西并坚持使用模式。 Remember to be sure that you make your app MT Safe also especially if you are accessing the same resources/variables etc... from different threads, regardless of which threading algorithm you use. 请记住确保您的应用程序MT安全,特别是如果您从不同的线程访问相同的资源/变量等,无论您使用哪种线程算法。

No. And there actually isn't much in the way of memory or performance inefficiencies when mixing approaches; 不会。混合方法实际上并没有太多记忆或性能低效的方法; by default tasks use the same thread pool that thread pool threads use. 默认情况下,任务使用线程池线程使用的相同线程池。

The only significant disadvantage of mixing both is lack of consistency in your codebase. 混合两者的唯一显着缺点是代码库缺乏一致性。 If you were to pick one, I would use TPL since it is has a rich API for handling many aspects of multi-threading and takes advantage of async/await language features. 如果你选择一个,我会使用TPL,因为它有一个丰富的API来处理多线程的许多方面,并利用async / await语言功能。

Since your usage is divided down module lines, you don't have much to worry about. 由于您的使用是按模块行划分的,因此您不必担心。

There shouldn't be any scheduling problems as such, but of course it's better to use Tasks and let the Framework decide what to do with the scheduled work. 不应该有任何调度问题,但当然最好使用Tasks并让Framework决定如何处理预定的工作。 In the current version of the framework (4.5) the work will be queued through the ThreadPool unless the LongRunning option is used, but this behaviour may change in future of course. 在当前版本的框架(4.5)中,除非使用LongRunning选项,否则工作将通过ThreadPool排队,但此行为可能在将来发生变化。

Verdict: Mixing Tasks and ThreadPool isn't a problem, but for new applications it's recommended to use Tasks instead of queueing work items directly on the ThreadPool (one reason for that is ThreadPool isn't available in Windows 8 Runtime (Modern UI apps). 结论:混合任务和ThreadPool不是问题,但对于新应用程序,建议使用Tasks而不是直接在ThreadPool上排队工作项(原因之一是ThreadPool在Windows 8运行时(现代UI应用程序)中不可用) 。

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

相关问题 线程池未按预期使用线程? - Thread-pool don't use threads as expected? 未在线程池线程上安排任务继续 - Task continuation was not scheduled on thread-pool thread 如何告诉线程池在STA线程上运行委托? - How to tell thread-pool to run a delegate on a `STA` thread? 任务继续计划在非线程池线程中进行。 为什么? - Task continuation was scheduled to non thread-pool thread. Why? 异步代码、共享变量、线程池线程和线程安全 - Asynchronous code, shared variables, thread-pool threads and thread safety 限制Async方法的并行性,而不是阻塞线程池线程 - Limit parallelism of an Async method and not block a Thread-Pool thread 使用线程池而不是异步IO的HttpClient.SendAsync? - HttpClient.SendAsync using the thread-pool instead of async IO? ThreadPool.RegisterWaitForSingleObject是阻止当前线程还是线程池线程? - Does ThreadPool.RegisterWaitForSingleObject block the current thread or a thread-pool thread? .NET 线程池是否应该在启动时创建最少数量的工作线程? - Should .NET thread-pool create the minimum amount of worker threads at start-up? 线程池中的排队任务正在退出,没有被释放回池中 - Queued tasks in thread pool are exiting, not being released back into pool
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM