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