There's a ton of questions and answers already (Exhibit A) about how to wake up a sleeping thread from another thread or how to have a thread sleep until it receives an instruction (same thing but described from another direction), but my question is more specifically about what is actually happening under the hood.
Is the only way to do this for the thread to itself choose to sleep and then check its queue, or some variable somewhere when it wakes, then sleep again?
Another method that I've seen mentioned is to use a mutex. Have the thread try to lock a locked mutex and it'll sleep then magically wake up when the mutex is unlocked by its original locker. But isn't that just doing the same thing under the hood? I.e. going to sleep for x period of time then waking up at intervals to check if its still locked?
Is there any kind of wake functionality that doesn't require sleeping for set intervals? Like some kind of OS-level push? For example, the OS must be keeping track of when it needs to wake the sleeping thread, right? So in theory if you could change that number to 0 or 1 then it would wake up sooner. That makes sense.
The reason why I ask is because there's a tradeoff between how long it takes to wake up, and the amount of resources its using. The longer it sleeps between checks, the slower it is to respond, and the shorter it waits the more cycles it wastes checking something that another part of the application already knows has not changed. Seems inefficient.