Struggling with threading structure

not sure what you mean in this context. :stuck_out_tongue: sorry. :man_shrugging:

Its typical for hardware that needs to detect rare pulses to latch a bit in a register when the pulse happens.
You then check for the bit being set, or better yet get an interrrupt to tell you the bit is set.
After you read the bit you write a bit in another register to clear it.

With such hardware you are freed from the real-time polling for the pulse.

1 Like

huh. i did not know that! fantastic! :partying_face: i’ll ask the hardware manufacturer. man, that would make life a hell of a lot easier for sure! thank you! :smiley:

For the code, in this case it’s probably simpler/more suitable to define a lock (threading.Lock()) and then then use with lock: (or with self.lock if you wrap the readDIN functions in a class) both to increment and to read the counter (for the read the locking may not be needed, depending on how those threads are actually used together).

Instead of your own counter, there is also an atomic-counter package you could use (you can look it up on the Pypi); this takes care of the locking, so should by itself be completely thread-safe.

And even if you don’t have the option to do this from the manufacturer, it might be possible to actually wire up a simple latch circuit in hardware yourself (depending on space constraints and such).