I have no way to wake up a thread being blocked by poll.poll() function . Could someone help me ?
The way to handle this is to have an extra file descriptor included in the list of descriptors passed to poll(). For that descriptor wait for a read to be ready. Have any other thread which wants to awaken the thread waiting on poll() write to that extra descriptor. At that point, the thread which called poll() resumes execution, sees that the extra descriptor is that which awakened it, and does whatever.
The normal way to get this extra file descriptor initially is to open an unnamed pipe with pipe(). That way you have two descriptors: the one you hand the read wait in poll() on and the other which you write to to awaken the thread waiting on poll().
There are many legitimate reasons to want to wake up a blocking poll(2) or select(2) before it times out. Any time you need to add or remove a socket from the fd list being polled, you need to break out of the wait. If you rely on the timeout, the timeout has to be short to be responsive, but short timeouts can result in a lot of needless thrashing.
For Linux anyway, you can use eventfd(2) to create a file descriptor to add to your fd list. When you need to break out of the wait, write to the eventfd.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With