It greatly depends on how your program works, but in principle, the easiest way would be to save a dream and check if the user changed the song before sending this data (it returns after sleep). Thus, instead of “sleeping better,” the goal would be to “verify that the data you are sending is valid before it is sent.”
Another possibility is to wait on epoll , using either a sleep timeout, or even better on timerfd , and notify the song change using eventfd . The advantage of this is that it is “free” if you still need a reliable notification about the exchange of information and readiness, which you most likely do (obviously, you must have at least one additional GUI thread, or the user will not be able to change songs while you block).
source share