Keeping a thread idle for a while
-
Hi all, I have a thread in my application. When the thread is running its eating up the CPU. My thread code is like:
ThreadProcess(LPVOID)
{
while(TRUE)
{
if(flag==true) //This thread waits for the flag to be set
{
ExecFunc();}
}
}
If the flag is set then it will execute the function.This flag is set in other function. So I want to keep this thread idle until the flag is set. How can I do it.Please help me.
Regards, Sunil Kumar
-
Hi all, I have a thread in my application. When the thread is running its eating up the CPU. My thread code is like:
ThreadProcess(LPVOID)
{
while(TRUE)
{
if(flag==true) //This thread waits for the flag to be set
{
ExecFunc();}
}
}
If the flag is set then it will execute the function.This flag is set in other function. So I want to keep this thread idle until the flag is set. How can I do it.Please help me.
Regards, Sunil Kumar
For waiting purpose you can use WaitForSingleObject(Just refer the usage http://msdn.microsoft.com/en-us/library/ms687032(VS.85).aspx[^]) Just create a named event using CreateEvent(refer http://msdn.microsoft.com/en-us/library/ms682396(VS.85).aspx[^]) Now you can wait till that event be signaled from your side. And for signaling you can use SetEvent(http://msdn.microsoft.com/en-us/library/ms686211(VS.85).aspx[^])
Величие не Бога может быть недооценена.
-
Hi all, I have a thread in my application. When the thread is running its eating up the CPU. My thread code is like:
ThreadProcess(LPVOID)
{
while(TRUE)
{
if(flag==true) //This thread waits for the flag to be set
{
ExecFunc();}
}
}
If the flag is set then it will execute the function.This flag is set in other function. So I want to keep this thread idle until the flag is set. How can I do it.Please help me.
Regards, Sunil Kumar
Don't use a boolean but use an event instead (and use the WaitForSingleObject[^] function). There's also a good tutorial about threads here[^]
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++ -
Hi all, I have a thread in my application. When the thread is running its eating up the CPU. My thread code is like:
ThreadProcess(LPVOID)
{
while(TRUE)
{
if(flag==true) //This thread waits for the flag to be set
{
ExecFunc();}
}
}
If the flag is set then it will execute the function.This flag is set in other function. So I want to keep this thread idle until the flag is set. How can I do it.Please help me.
Regards, Sunil Kumar
If you expect the wait time to be relatively long, then use one of those WaitFor... functions. If the wait time would be very low, then a spin loop is an ideal candidate.
It is a crappy thing, but it's life -^ Carlo Pallini