-
January 21st, 2009, 07:40 AM
#1
How to avoid resource consumption
I am just a beginner in mutlithreading.
I have a main program that creates a single thread, which code looks like this:
MyThread
{
while (threadActive)
{
if (workToBeDone)
doTheWork();
}
return 0;
}
There is no need of a mutex because the variables used in 'doTheWork()' are local, only used there. The booleans 'threadActive' and 'workToBeDone' are set my the main program: when it sets 'threadActive' to false, the thread ends, and it sets 'workToBeDone' when the 'doTheWork' function needs to be called.
It works fine, but I have a problem:
When the thread is active, it takes 100% of the CPU for one of the cores, even if no work needs to be done and 'workToBeDone' remains false. The 'while' loop takes all the CPU. SO I thought about adding a 'Sleep' in the loop, but the problem is that it delays the invocation of 'doTheWork' when a work needs to be done (this is a problem because it is related to real-time graphics).
Is there a solution to avoid taking 100% of the CPU when no work has to be done and the thread just loops?
Thank you,
Eric
-
January 21st, 2009, 08:32 AM
#2
-
January 21st, 2009, 11:44 AM
#3
Re: How to avoid resource consumption
This would work, but if 'work' is set to true while the thread is sleeping, there can be a delay of 100 ms (in the worst case) before the work begins. This is not acceptable because of the constraints of my application.
Thanks for your help !!
Eric
-
January 21st, 2009, 12:51 PM
#4
Re: How to avoid resource consumption
You typically create a manual reset event for "thread exit", and either a manual reset or auto reset event for "do work". Then the thread uses WaitForMultipleObjects() to detect when the events are in a signaled state.
http://msdn.microsoft.com/en-us/libr...15(VS.85).aspx
gg
-
January 21st, 2009, 01:21 PM
#5
Re: How to avoid resource consumption
I did say that you could change the value of the nTime variable. On Windows XP you can get about 10ms and on Vista 1ms sleeps. Those are the perspective quantums. I agree with codeplug on the waitformultipleobjects(), although you should consider how time critical your code really is.
Originally Posted by Rocky651
This would work, but if 'work' is set to true while the thread is sleeping, there can be a delay of 100 ms (in the worst case) before the work begins. This is not acceptable because of the constraints of my application.
Thanks for your help !!
Eric
ahoodin
To keep the plot moving, that's why.
-
January 21st, 2009, 05:17 PM
#6
Re: How to avoid resource consumption
Originally Posted by Codeplug
You typically create a manual reset event for "thread exit", and either a manual reset or auto reset event for "do work". Then the thread uses WaitForMultipleObjects() to detect when the events are in a signaled state.
http://msdn.microsoft.com/en-us/libr...15(VS.85).aspx
gg
Indeed, using WaitForSingleObject to wait for the trigger (an event) that starts the work is much better. The CPU usage is now optimal and my thread is as fast as before !!
Thank for your help !!
Eric
-
January 27th, 2009, 12:45 AM
#7
Re: How to avoid resource consumption
In general event-based programming is better than busywaiting, yes. Another way to accomplish this would be to have a condition variable which the loop waits on, that gets signaled whenever it's time to move on to another task.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|