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:
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With