LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   CPU usage 100% due to loops in multiple threads (https://www.linuxquestions.org/questions/linux-general-1/cpu-usage-100-due-to-loops-in-multiple-threads-746352/)

Manjunath1847 08-10-2009 01:52 AM

CPU usage 100% due to loops in multiple threads
 
I have an linux application (written in C++), which spawns 60 threads, and all these 60 threads executes an nested loop which iterates around 100 times. Number of iterations is same for all the threads. When I spawn only one thread time taken by application is just 10-20 seconds. When I run with 60 threads time taken increases to more than 1 minute. Also CPU usage goes to 100% and stays there for more than 1 minute till the application exits. When CPU usage is 100% I cannot run any other commands. Since I need to run this application for every 5 minutes I keep hitting this problem. Please can anyone suggest a solution for this. How can I avoid loop? Is there any way to avoid 100% CPU usage inspite of this loop. Please HELP.

Thanks in advance.

chrism01 08-10-2009 01:57 AM

Depends what your loops do.
Do you REALLY need 60 copies of the same code all running at once? Its obviously cpu bound, which suggests its some kind of calculation.
We need more info.

Manjunath1847 08-10-2009 02:06 AM

Yes I need 60 copies of same code running at same time. My application collects the device data from 60 different devices and loops over the collected data to get the different informations. This data collections happens for every 5 min.

chrism01 08-10-2009 02:11 AM

Well, then it should be on a dedicated server system so you don't need to run other (urgent) cmds.

You've got 2 options:
1. slow the processing down a bit so other cmds can be run eg
You could try adding a short sleep at the bottom of each loop to allow other processes to access the cpu.

2. If you REALLY need the processing done in seconds instead of 1 minute, use HW with more cpus.

Manjunath1847 08-10-2009 02:45 AM

Thanks chrism01.
Adding small sleep will work fine when number of threads is less than 30. When running more threads sleep also will not help. Since many threads are fighting for CPU, sleep effect is getting nullified by other threads.


All times are GMT -5. The time now is 12:50 AM.