Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 (i.e. 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.
like image 262
Roey Nissim Avatar asked Apr 24 '12 06:04

Roey Nissim


People also ask

Is multi threading possible in C?

C does not contain any built-in support for multithreaded applications. Instead, it relies entirely upon the operating system to provide this feature. This tutorial assumes that you are working on Linux OS and we are going to write multi-threaded C program using POSIX.

How do I reduce CPU thread usage?

The best solution to limiting the cpu usage for a process or thread is to make sure that the thread or process uses less cpu. That can best be done by improving the efficiency of the code, or by calling it less often. The aim is to make sure that the process doesn't continually consume all of its available time slice.

Does multithreading increase CPU usage?

Although you can take advantage of multithreading to perform several tasks simultaneously and increase the application's throughput, it should be used judiciously. Incorrect usage of multithreading may result in high CPU usages or increased CPU cycles and can drastically reduce your application's performance.

What is the correlation between threads and CPU utilization?

It depends on the jobs the threads are doing. A program with one thread can consume 100% of CPU and a program with lots of threads can consume less. If you are looking for an optimized relation between threads and job done, you must study your case, and possibly found an empiric solution.


1 Answers

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.

like image 128
weismat Avatar answered Sep 20 '22 23:09

weismat