简体   繁体   中英

multi-thread CPU usage in C#

My Program uses predetermined number of threads that each do independent work. I use i7-2600 CPU but I shut down the hyper-thread module so it runs 4 threads on 4 cores. When I run the program with 1 thread the CPU usage is 25% which is perfect since 1 thread is fully used, but when I run 4 or 3 thread I only get 60% CPU, why?

Like I mentioned before the threads are totally independent (there are no locks and no contentions) also when I run the program 4 times with 1 thread I get 100% CPU usage (ie when its 4 processes of 1 thread for each process I get proper CPU usage)

Any ideas?

some more info:

  1. I'm not using I/O while processing all the needed data is loaded into to the memory, each thread loads its own data prior to processing.
  2. I do load the data from a database (using oleDb) but the issue I'm describing happens after the loading while all threads are processing (no loading is done).
  3. The numbers are:
    • 2 threads around 40% instead of 50 (85% for each thread).
    • 3 threads around 50% instead of 75 (65% for each thread).
    • 4 threads around 60% instead of 100 (60% for each thread).
  4. I use i7 2600 with 16GB memory but the memory usage on that process doesn't come close to that.
  5. I ran windows performance monitor to look for contentions there are none.

You may try to shift your program from workstation to server garbage collection mode. Currently you may use just one garbage collection thread.
The setup is explained here .
See this thread for an explanation/further details.
Do not expect 100 % CPU load after the change, but you will get closer to 100% and increase the spead of it.

The concurrency visualizer will help you here. You will see when each thread is executing, and if all threads are not executing you can see exactly why a specific thread is waiting. If you generate a lot of garbage, the GC may keep threads blocked leading to less than linear scaling. But you will only find out by making a real investigation.

I had the same issue while working with .NET Framework v2.0 then i changed the projects target framework to v4.0 it started with 100 % CPU load

Edit: You can watch the CPU usage per threads, simply with

Sysinternals Process Explorer

or

Process Hacker

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM