Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multithreading improvements in .NET 4

I have heard that the .NET 4 team has added new classes in the framework that make working with threads better and easier.

Basically the question is what are the new ways to run multithreaded tasks added in .NET 4 and what are they designed to be used for?

UPD: Just to make it clear, I'm not looking for a single way of running parallel tasks in .NET 4, I want to find out which are the new ones added, and if possible what situation would each of them be best suited for..

like image 760
Artiom Chilaru Avatar asked Apr 24 '10 13:04

Artiom Chilaru


People also ask

Is .NET multi threaded?

With . NET, you can write applications that perform multiple operations at the same time. Operations with the potential of holding up other operations can execute on separate threads, a process known as multithreading or free threading.

What is multithreading in .NET with example?

Multi-threading is a process that contains multiple threads within a single process. Here each thread performs different activities. For example, we have a class and this call contains two different methods, now using multithreading each method is executed by a separate thread.

What are threads multithreading in C#?

Multithreading is a feature provided by the operating system that enables your application to have more than one execution path at the same time. Multithreading is a feature provided by the operating system that enables your application to have more than one execution path at the same time.

What is multithreading in MVC?

The first is that every ASP.NET application (MVC or otherwise) is inherently multi-threaded: Each request will be processed on a separate thread, so you are automatically in a multi-threading situation and must consider this with any shared access to data (e.g. statics, etc.).


2 Answers

With the lack of responses, I decided to evaluate on the answers below with that I've learned.. As @Scott stated, .NET 4 added the Task Parallel Library which adds a number of innovations, new methods and approaches to parallelism.

  • One of the first things to mention is the Parallel.For and Parallel.ForEach methods, which allow the developer to process multiple items in multiple threads. The Framework in this case will decide how many threads are necessary, and when to create new threads, and when not to.
    This is a very simple and straightforward way to parallelize existing code, and add some performance boost.
  • Another way, somewhat similar to the previous approaches is using the PLINQ extenders. They take an existing enumeration, and extend it with parallel linq extenders. So if you have an existing linq query, you can easily convert it to PLINQ. What this means is all the operations on the PLINQ enumerable will also take advantage of multiple threads, and filtering your list of objects using a .Where clause, for example, will run in multiple threads now!
  • One of the bigger innovations in the TPL is the new Task class. In some ways it may look like the already well known Thread class, but it takes advantage of the new Thread Pool in .NET 4 (which has been improved a lot compared on previous versions), and is much more functional than the regular Thread class. For example you can chain Tasks where tasks in the middle of the chain will only start when the previous ones finish. Examples and in-depth explanation in a screencast on Channel 9
  • To enhance the work with Task classes, we can use the BlockingCollection<>. This works perfectly in situations where you have a producer-consumer scenario. You can have multiple threads producing some objects, that will then be consumed and processed by consumer methods. This can be easily parallelised and controlled with the Task factory and the blocking collection. Useful screencast with examples from Channel 9
    These can also use different backing storage classes (ConcurrentQueue, ConcurentStack, ConcurrentBag), which are all thread safe, and are different in terms of element ordering and performance. Examples and explanations of them in a different video here
  • One more new thing that has been added (which probably isn't part of the TPL, but helps us here anyway) is the CountdownEvent class, which can help us in "task coordination scenarios" (c). Basically allows us to wait until all parallel tasks are finished. Screencast with example usage on Channel 9

You can see a number of screencasts and videos on Channel 9 that are tagged with "Parallel Computing"

like image 195
Artiom Chilaru Avatar answered Sep 29 '22 14:09

Artiom Chilaru


Yes, .NET 4 added the Task Parallel Library which, at a high level, adds support for:

  • running parallel loops with Parallel.For and Parallel.ForEach
  • create or run tasks using Parallel.Invoke or the Task class
  • PLINQ (parallel LINQ to Objects)

Answering the update to the original question...

The TPL is the preferred way of writing parallel tasks using .NET 4. You can still create threadpool items yourself, and do all of the same "manual" threading techniques you could before. The thing to keep in mind is that the entire threadpool (and pretty much everything threading related) has been rewritten to take advantage of the TPL. This means that even if you create a threadpool item yourself you still end up using the TPL, even if you don't know it. The other thing to keep in mind is that the TPL is much more optimized, and will scale more appropriately based on the number of available processors.

As for knowing what situation each of them would be best suited for, there is no "silver bullet" answer. If you were previously queueing your own threadpool item (or otherwise doing something multi-threaded) you can modify that portion of your code to use the TPL without any consequences.

For things like parallel loops or parallel queries, you will need to analyze the code and the execution of that code to determine if it is appropriate to be parallelized.

like image 41
Scott Dorman Avatar answered Sep 29 '22 13:09

Scott Dorman