C# Tutorials - Herong's Tutorial Examples - Version 3.30, by Dr. 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.
Last update: 2015.
Table of Contents