Fundamentals of Microsoft .NET Programming: Multiprocessing
Task Parallel Library
How you create multiple threads depends on the operating system and language you are using. Microsoft’s Task Parallel Library (TPL) is a specific library of tools that makes running parallel threads relatively easy for Microsoft .NET applications.
The following list summarizes the main tools provided by the TPL:
Parallel.Invoke Executes several pieces of code at the same time.
Parallel.For Executes the same pieces of code several times in parallel, with different numbers as parameters. For example, it might invoke some code to produce frames in an animated movie where the parameters 1, 2, 3, and so on are passed to the code so it knows which frame number to generate.
Parallel.ForEach Executes the same pieces of code several times in parallel, with different arbitrary values as parameters. This is similar to Parallel.For except that the code receives arbitrary values specified by the program as inputs rather than numbers in a sequence. For example, the program could pass each thread a separate image to manipulate. The threads could then perform image processing techniques on the images, creating embossed images.
These three TPL operations provide a relatively simple way for a program to use multiple threads. These may not handle every scenario that you can imagine, and threads still may run into race, lock, deadlock, and other parallel issues, but these are fairly easy to use.
The TPL is also designed to use multiple cores, if they are available, without imposing too much overhead on single-core systems, so you can run the same program on different computers and expect reasonable performance, whether the computer has 1 core or 16.