Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decrease CPU Usage when it reaches to 100% when using while(1) loop

I am working on UDP Server/Multiple Client Application.

There are multiple threads handling multiple clients.

There is one single thread which keeps on sending KEEPALIVE Messages to each active clients. Since this thread is in while(1) so CPU Usage reaches to 100%.

Since I want this functionality to go on, I have used a while(1) thread. I also tried adding a sleep after each execution of while but I don't think sleep() frees the CPU. Is there any way I can decrease CPU Usage for a specific time. e.g after a single execution of while, I can free up the CPU for like 10 secs and then continue back to while.

Please help me. Thanks a lot in advance.

like image 628
Ayse Avatar asked Dec 07 '25 23:12

Ayse


1 Answers

sleep - Suspends the execution of the current thread until the time-out interval elapses. And gives processor to other threads which are ready to run.

source : http://msdn.microsoft.com/en-us/library/windows/desktop/ms686298(v=vs.85).aspx

So, just sleep does it all you need.

like image 183
VoidPointer Avatar answered Dec 10 '25 12:12

VoidPointer