This is one of those questions that seems to fall into the category of βnaively visible, but probably wrong.β Of course, I'm struggling to find a solution that works in all cases. It seems that this should be a problem that occurs all the time.
I have a "hardware" thread and a "processing" thread.
The hardware thread runs in a private binary library that I do not have access to.
The processing thread registers a callback with the hardware thread in order to receive notifications of some (rare) events associated with changes in the state of the hardware.
I want to be able to notify the event loop in a thread that has changed state. I would like to do this without requiring a dependency on the external library or in a non-portable way, and without using locks (since I have no idea when the hardware thread can again notify the processing thread).
So now Iβm thinking about how to solve this problem, something like this:
Clearly, this will break if ever the update wraps local_update and just reaches the local_update value before the processing_function is called (although I can almost certainly assume that this will never happen).
Am I missing something subtle (or not so subtle) here?
Is there a better way to solve this problem?
A callback can be considered called only from one thread (hardware thread).
source share