Just to let you know what’s going on, a hard loop consumes the allocation of your processor thread just like any other code executed by your thread. The only difference is that it does nothing. Well, I think creating a heart tone is something ... You probably will never want to do this unless you care about the cost of the context switch, and you know for sure that the wait time will be much shorter than your stream time, which is about 1 ms. I don’t know if the dream is as bad as the crop (AFAIK output gives you the thread of the thread list thread to activate at your priority level), but both at least carry a penalty for the context switch. There is a cost of context switching. A context switch is what happens when your time stream ends. This may end because you ended it with damage or sleep, or if the core finished it, having previously released you. Read about SMP and spinlocks to learn more about context switches and when a compressed loop applies.
In addition, when you sleep, you do not know exactly when you will wake up again. therefore, one of the reasons you might not want to sleep is to do something quickly. You can wait a couple of ms before being ported.
Everyone else has provided you with the pthread_cond_wait solution, which requires locking through mutexes and looks simple and simple. Performance may be appropriate for your needs, but relatively slow compared to a solution using signals ( sigwait and pthread_kill ). Difficulty will sneak up on you.
It is not discussed here why you block the mutex before testing if you need to wait for the state. The reason is that such code has a flaw:
while (x <= y) { pthread_cond_wait(&cond, &mut); }
Your thread can check (X <= Y), see that it should wait, but is preceded before it joins the condition value. Some other thread that has just changed x or y signals the state in this temporary sheet before the first thread joins the condition. Thus, the status signal is lost. So what happens, wherever you change the variables that improve the signal, you need to add locks.
pthread_mutex_lock(&mut); if (x > y) pthread_cond_broadcast(&cond); pthread_mutex_unlock(&mut);
This may mean that you add these mutex locks in many different places, and because of this, your code becomes more complex and slow. The possibility of a hanging thread awaiting conditions is always present. In solving the cycle, test, sleep, none of these problems exist. Therefore, if the waiting time is known to be short, using sleep in a cycle is probably the perfect solution. Especially if you can sleep for a second or more between trials. If you can, don’t worry about mutexes and conditions. If the sleep time is short, for example, 1 ms, and the wait time is long, for example, minutes or hours, then in the end you lose resources, constantly waking up and returning to sleep. You must judge.
Also keep in mind that sometimes the core will try to wake the waiter's flow, and in other cases there will be a delay. If the thread wakes up too early, it will wake up when the mutex is locked and will immediately return to sleep until the mutex is unlocked. If this becomes a problem, report it as follows:
pthread_mutex_lock(&mut); if (x > y) { pthread_mutex_unlock(&mut); pthread_cond_broadcast(&cond); } else pthread_mutex_unlock(&mut);
Thank you for using user576875 who copied the code samples.