Skip to main content
deleted 21 characters in body; edited title
Source Link
Jim
  • 3k
  • 2
  • 22
  • 29

Processor Affinity and Thread Creation by Thread Pool in C#

This question is based on Jeffrey Richter's threading class in Microsoft Virtual Academy.

As per my understanding if I set my processor affinity to use only 1 processor as shown below

Int32 affinity = 1; Process.GetCurrentProcess().ProcessorAffinity = (IntPtr)affinity; for (int i = 0; i < 200 ; i++) { ThreadPool.QueueUserWorkItem(ActiveWorker); } 

ActiveWorker()ActiveWorker() is a CPU bound function that just do some CPU bound computation that takes around 100ms. When I count number of threads created its around 5. But as per my understanding since CPU affinity is set to use only 1 processor, thread pool should not create more than 1 thread. Any idea? In Jeffrey's presentation it was showing it created only 1 thread for entire 200 workitem, but when I ran the same sample in my PC, I can see 5 thread created. (My PC has 2 cores)

thanks
Jithesh

Processor Affinity and Thread Creation by Thread Pool in C#

This question is based on Jeffrey Richter's threading class in Microsoft Virtual Academy.

As per my understanding if I set my processor affinity to use only 1 processor as shown below

Int32 affinity = 1; Process.GetCurrentProcess().ProcessorAffinity = (IntPtr)affinity; for (int i = 0; i < 200 ; i++) { ThreadPool.QueueUserWorkItem(ActiveWorker); } 

ActiveWorker() is a CPU bound function that just do some CPU bound computation that takes around 100ms. When I count number of threads created its around 5. But as per my understanding since CPU affinity is set to use only 1 processor, thread pool should not create more than 1 thread. Any idea? In Jeffrey's presentation it was showing it created only 1 thread for entire 200 workitem, but when I ran the same sample in my PC, I can see 5 thread created. (My PC has 2 cores)

thanks
Jithesh

Processor Affinity and Thread Creation by Thread Pool

This question is based on Jeffrey Richter's threading class in Microsoft Virtual Academy.

As per my understanding if I set my processor affinity to use only 1 processor as shown below

Int32 affinity = 1; Process.GetCurrentProcess().ProcessorAffinity = (IntPtr)affinity; for (int i = 0; i < 200 ; i++) { ThreadPool.QueueUserWorkItem(ActiveWorker); } 

ActiveWorker() is a CPU bound function that just do some CPU bound computation that takes around 100ms. When I count number of threads created its around 5. But as per my understanding since CPU affinity is set to use only 1 processor, thread pool should not create more than 1 thread. Any idea? In Jeffrey's presentation it was showing it created only 1 thread for entire 200 workitem, but when I ran the same sample in my PC, I can see 5 thread created. (My PC has 2 cores)

Source Link
DevMonk
  • 435
  • 1
  • 9
  • 24

Processor Affinity and Thread Creation by Thread Pool in C#

This question is based on Jeffrey Richter's threading class in Microsoft Virtual Academy.

As per my understanding if I set my processor affinity to use only 1 processor as shown below

Int32 affinity = 1; Process.GetCurrentProcess().ProcessorAffinity = (IntPtr)affinity; for (int i = 0; i < 200 ; i++) { ThreadPool.QueueUserWorkItem(ActiveWorker); } 

ActiveWorker() is a CPU bound function that just do some CPU bound computation that takes around 100ms. When I count number of threads created its around 5. But as per my understanding since CPU affinity is set to use only 1 processor, thread pool should not create more than 1 thread. Any idea? In Jeffrey's presentation it was showing it created only 1 thread for entire 200 workitem, but when I ran the same sample in my PC, I can see 5 thread created. (My PC has 2 cores)

thanks
Jithesh