Multi-threading libraries for .NET

后端 未结 10 1950
长发绾君心
长发绾君心 2021-02-14 08:39

I used multiple threads in a few programs, but still don\'t feel very comfortable about it.

What multi-threading libraries for C#/.NET are out there and which advantages

相关标签:
10条回答
  • 2021-02-14 09:23

    I have written a lot of threading code in my days, even implemented my own threading pool & dispatcher. A lot of it is documented here:

    http://web.archive.org/web/20120708232527/http://devplanet.com/blogs/brianr/default.aspx

    Just realize that I wrote these for very specific purposes and tested them in those conditions, and there is no real silver-bullet.

    0 讨论(0)
  • 2021-02-14 09:27

    There are various reasons for using multiple threads in an application:

    • UI responsiveness
    • Concurrent operations
    • Parallel speedup

    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.

    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.

    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.

    0 讨论(0)
  • 2021-02-14 09:28

    My advise would be to get comfortable with the thread pool before you move to any other libraries. A lot of the framework code uses the thread pool, so even if you happen to find The Best Threads Library(TM), you will still have to work with the thread pool, so you really need to understand that.

    You should also keep in mind that a lot of work has been put into implementing the thread pool and tuning it. The upcoming version of .NET has numerous improvements triggered by the development the parallel libraries.

    In my point of view many of the "problems" with the current thread pool can be amended by knowing its strengths and weaknesses.

    0 讨论(0)
  • 2021-02-14 09:30

    You don't have to use the threadpool explicitly, you can use BeginInvoke-EndInvoke if you need async calls. It uses the threadpool behind the scenes. See here: http://msdn.microsoft.com/en-us/library/2e08f6yc.aspx

    0 讨论(0)
提交回复
热议问题