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:
2 CPUs with Multi Threads

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:
Processor Affinity Setting

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:
1 CPU with Multi Threads

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

 About This Book

 Introduction of C# (C Sharp)

 Data Type and Variables

 Logical Expressions and Conditional Statements

 Arrays and Loop Statements

 Data Type Features

 Floating-Point Data Types

 Passing Parameters to Methods

 Execution Environment Class

 Visual C# 2010 Express Edition

 Class Features

 C# Compiler and Intermediate Language

 Compiling C# Source Code Files

 MSBuild - Microsoft Build Engine

 Memory Usages of Processes

Multithreading in C#

 What Is Multithreading?

 System.Threading.Thread Class

 Creating and Running Threads

 Threads to Run Instance Methods

 Performance Impact with Multiple Threads

Multi-Thread Programs on Multi-CPU Systems

 Maximum Number of Threads in a Program

 Async Feature from C# 5

 System.IO.FileInfo Class

 System.Diagnostics.FileVersionInfo Class

 WPF - Windows Presentation Foundation

 Partial Classes and Partial Methods

 Outdated Tutorials

 References

 PDF Printing Version