多线程库.NET [英] Multi-threading libraries for .NET
问题描述
我用多个线程在几个项目,但仍然感觉不很舒服了。
I used multiple threads in a few programs, but still don't feel very comfortable about it.
什么多线程的C#库/。NET是在那里,做一件具有比其他哪些优势?
What multi-threading libraries for C#/.NET are out there and which advantages does one have over the other?
通过多线程库我的意思是一切,这有助于使多线程编程更容易。
By multi-threading libraries I mean everything which helps make programming with multiple threads easier.
什么.NET integratet(即类似线程池)你定期使用? 你会遇到哪些问题?
What .NET integratet (i.e. like ThreadPool) do you use periodically? Which problems did you encounter?
推荐答案
有各种各样的原因,使用多线程应用程序中的:
There are various reasons for using multiple threads in an application:
- 在用户界面的响应
- 并发操作
- 并行加速
该选择的方法取决于你正在努力做的事情。对于用户界面的响应,可以考虑使用的BackgroundWorker
例如。
The approach one should choose depends on what you're trying to do. For UI responsiveness, consider using BackgroundWorker
, for example.
有关并发操作(如服务器:一些不的有无的是平行的,但可能确实需要在单核系统甚至并发),考虑使用线程池或如果任务是长期存在的,你需要他们中的很多,可以考虑使用每个任务一个线程。
For concurrent operations (e.g. a server: something that doesn't have to be parallel, but probably does need to be concurrent even on a single-core system), consider using the thread pool or, if the tasks are long-lived and you need a lot of them, consider using one thread per task.
如果你有一个所谓的易并行问题,可以很容易地分割成小的子问题,可以考虑使用工作线程池(如多线程CPU核心),该拉的任务从一个队列。微软任务并行库(TPL)可能会有所帮助。如果作业可以pssed为一元流计算容易EX $ P $(即在LINQ查询,在转换和聚合等方面的工作),并行LINQ(同一个链接),它运行在TPL的顶部可能会有所帮助。
If you have a so-called embarrassingly parallel problem that can be easily divided up into small subproblems, consider using a pool of worker threads (as many threads as CPU cores) that pull tasks from a queue. The Microsoft Task Parallel Library (TPL) may help here. If the job can be easily expressed as a monadic stream computation (i.e. with a query in LINQ with work in transformations and aggregations etc.), Parallel LINQ (same link) which runs on top of TPL may help.
有其他的方法,如在二郎看出,这是演员式的并行更难在.NET中,由于缺乏一个绿色的线程模型的有效实施或手段来实现相同的,如CLR支持的延续。
There are other approaches, such as Actor-style parallelism as seen in Erlang, which are harder to implement efficiently in .NET because of the lack of a green threading model or means to implement same, such as CLR-supported continuations.
这篇关于多线程库.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!