Should .Net 4.0 Tasks have always been the preferred method for multi-threaded applications?

I read about a parallel task library , and the article said:

In the .NET Framework 4, tasks are the preferred API for writing multi-threaded, asynchronous, and parallel code.

But he also says that they use ThreadPool backstage. What I find difficult to understand is if Tasks should be used only when you use ThreadPool (and thus, "Thread versus Task" will be equivalent to "Thread versus ThreadPool"), or if Microsoft was intended for tasks to be used multiple threads are required anywhere, without the considerations inherent in the Thread versus ThreadPool dilemma.

So, should tasks be used wherever multiple threads are required?

+6
source share
3 answers

The advantage of using tasks is that you stream the processing of streaming, which, apparently, can perform tasks on the stream using less buggy, a more optimal solution. I know that certain task-based paradigms, such as PLINQ, allow you to hint what strategy to use at runtime so that you can directly handle the question “for Threadpool or not for Threadpool”.

Switching to this model is similar to switching to the GC-ed managed language compared to a language that requires you to clear your own memory. There will always be arguments in favor of the latter, but now garbage collection has become so optimized that it is practically no problem. Ideally, the runtime switching mechanism for tasks will evolve and improve. So, theoretically, your application written and compiled for .NET 4 can accelerate with better runtime implementations without further recompilation. In addition, multi-threaded code, as you know, is difficult to get correctly, so any mechanism that hides these details is good for the programmer.

Whether these benefits outweigh the potential losses, such as edge cases that the runtime cannot handle, is what should be considered in each case. I would certainly try not to optimize the earlier here.

+4
source

You can use TaskCreationOptions.LongRunning as a hint to tell TPL that your task may be more complex than setting up ThreadPool . But, yes, TPL seems to be the preferred method of multi-threaded programming with impatience. Microsoft even builds on top of it support for the new async and await keywords that are offered in Async CTP . This does not mean that you should completely abandon the old API Thread and ThreadPool . However, I personally believe that TPL does most of what I want in a more elegant API, and I tend to rely on it almost exclusively now.

+1
source

A task is an abstraction of a higher level than a thread or ThreadPool. Essentially, you install a function in a task and request that the runtime execute it as best as possible. You can have many dozens, if not hundrends of Tasks, which will be performed by a limited number of threads.

Using tasks, the developer creates as many tasks as necessary, combine them to create threads (workflows in F #) and control their cancellation without worrying about how the threads are distributed or used. It depends on the runtime to choose the best way to complete all tasks with a limited number of threads.

Tasks greatly simplify the implementation of parallel programming patterns. The ParallelExtensionExtras library provides methods for converting Begin / EndXXX pairs to tasks that can be bound, and a preliminary version of the task Iteration iteration, which is used by Async CTP to provide async / wait syntax. You can use the ConcurrentCollection of tasks to create a task queue, similar to the AsyncCall example in ParallelExtensionExtras, or go even further and create agents similar to Scala, Erlang or F #. Async CTP's DataFlow is another example of what you can create with tasks.

You should remember that although a typical laptop has 2 cores and a typical desktop has 4, small servers already have 8 or more cores, and soon they will have many more. Keeping all of these key manual workers by scheduling flows AND avoiding blocking can be a major headache.

0
source

Source: https://habr.com/ru/post/902069/


All Articles