You do not just sleep, you want to sleep on condition. The the answers with sleep won't help you.
You should never ever use spin wait. You should wait using thread synchronization primitives only.
Also, better not touch thread priority. If you do it right, it will not waste any CPU time. Here is how:
You need not sleep, you need to wait for
Thread code is:
Alternatively, you can use
with a parameter, which is a timeout.
On this call OS switches the thread off and never schedules it back to execution until it is waken up. The thread will spend exactly zero CPU time. The thread can be waken up only be
called from the other thread. That's it.