C# Tutorials - Herong's Tutorial Examples - v3.32, by Herong Yang
Multi-Thread Programs on Multi-CPU Systems
This section provides a tutorial example to see how multi-thread programs behave on multi-CPU systems and how to change processor affinity to control which processors to use for each application.
Since I have 2-CPU system, I want to see how multithreading application behave on a multi-CPU system.
1. Modify MultiThread.cs to set count = 2. Then compile and run MultiThread.exe. You will notice that your computer performance gets reduced after MultiThread.exe started. And you will this output:
22:09:58.4675000: Launched thread 1 22:09:58.5143750: Launched thread 2
2. Run "Task Manager", go to "Processes" tab, and click "CPU" column to see the
process with highest CPU usage first. As you can see from the picture below, MultiThread.exe
is on the top of the list and uses about 98% CPU resource:
3. Right-mouse click on MultiThread.exe and select "Set Affinity". You will see the "Processor Affinity" dialog box with this help text: "The Processor Affinity setting controls which CPUs the process will be allowed to execute on."
4. Uncheck "CPU 1" and keep "CPU 0" checked as shown below:
5. Click "OK" to save the change.
You will notice that your computer speed gets improved.
And the CPU usage of MultiThread.exe is changed
to about 50% as shown below:
I guess what happened here was:
From this test, we learned that multi-CPU systems are able to assign different CPUs to run different threads to run them in parallel.
Table of Contents
Logical Expressions and Conditional Statements
Visual C# 2010 Express Edition
C# Compiler and Intermediate Language
Compiling C# Source Code Files
MSBuild - Microsoft Build Engine
Threads to Run Instance Methods
Performance Impact with Multiple Threads
►Multi-Thread Programs on Multi-CPU Systems
Maximum Number of Threads in a Program
System.Diagnostics.FileVersionInfo Class
WPF - Windows Presentation Foundation