Multithreading

Started by
2 comments, last by ziplux 23 years, 3 months ago
I''m making a multithreaded program. I made a funciton that I want to run, like this:
  
DWORD WINAPI func(LPVOID arg)
{
while(bQuit != true)
{
// do stuff

}
}
  
WHen I use CreateThread the create a thread that runs that function, my CPU usage goes thru the roof to 100 percent. Is there another way to do this without sitting in a busy while loop? Thanks in advance. My Geekcode: "GCS d s: a14 C++$ P+(++) L+ E-- W+++$ K- w++(+++) O---- M-- Y-- PGP- t X R- tv+ b++ DI+(+++) D- G e* h!" Decode my geekcode! Geekcode.com
Visit our web site: Asylum Entertainment
My Geekcode: "GCS d s: a14 C++$ P+(++) L+ E-- W+++$ K- w++(+++) O---- M-- Y-- PGP- t XR- tv+ b++ DI+(+++) D- G e* h!"Decode my geekcode!Geekcode.com
Visit our web site:Asylum Entertainment
Advertisement
use sleep in the loop.

as long as your loop is running without ever relinquishing control of the thread, your cpu usage will always be 100%.

this isn''t always bad. you can give your thread a low priority and still use 100% of the cpu time. This way any time another process or thread needs the cpu, that thread will give the other process control.
worked like a charm, thanks AP.


My Geekcode: "GCS d s: a14 C++$ P+(++) L+ E-- W+++$ K- w++(+++) O---- M-- Y-- PGP- t X
R- tv+ b++ DI+(+++) D- G e* h!"
Decode my geekcode!
Geekcode.com


Visit our web site:
Asylum Entertainment
My Geekcode: "GCS d s: a14 C++$ P+(++) L+ E-- W+++$ K- w++(+++) O---- M-- Y-- PGP- t XR- tv+ b++ DI+(+++) D- G e* h!"Decode my geekcode!Geekcode.com
Visit our web site:Asylum Entertainment
You could also WaitOnSingleEvent or multiple events... it depends on what kinds of work is being done in the loop.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement