Thread pool and the degree of parallelism
This recipe will show how a thread pool works with many asynchronous operations, and how it is different from creating many separate threads.
Getting ready
To step into this recipe, you will need Visual Studio 2012. There are no other prerequisites. The source code for this recipe could be found in BookSamples\Chapter3\Recipe3
.
How to do it...
To learn how a thread pool works with many asynchronous operations and how it is different from creating many separate threads, perform the following steps:
- Start Visual Studio 2012. Create a new C# Console Application project.
- In the
Program.cs
file, add the followingusing
directives:using System; using System.Diagnostics; using System.Threading;
- Add the following code snippet below the
Main
method:static void UseThreads(int numberOfOperations) { using (var countdown = new CountdownEvent(numberOfOperations)) { Console.WriteLine("Scheduling work by creatingthreads"); for (int i=0; i<numberOfOperations...