I want to use the GPIO pins to wait for a button-press without using a CPU spin loop. My preferred way of using the GPIO pins is via the sysfs interface at /sys/class/gpio
, but it seems to me that there is an inherent race condition in doing so. Namely, if I understand the sysfs interface to GPIO correctly, it seems one must go through the following sequence:
- Read the
value
file to see whether the desired condition holds true. - If it does not (the usual case for the first iteration), poll the value file for
POLLPRI
to sleep until it changes state, and repeat from step 1.
However, in this procedure, there is a window of opportunity between steps 1 and 2 such that the button is not yet pressed when the value is read, but then pushed right before entering doing the poll
call, in which case this particular button press would effectively be missed.
I mean, I realize that a low-frequency event like button presses doesn't really have a high probability of triggering this race condition, but there are certainly more high-frequency events that could, and even regardless of that, it just seems ugly. Is there a way to avoid this problem?
sysfs
, but it's been deprecated and may not be around much longer.pigpio
is one of those libraries, and I see @joan has already answered, so you're in good hands.poll
call, and my question is about how to coordinate that with a point when I know that the button is not already pressed.