I have a bunch of objects, and each object has an unique ID.
I need to interlock the objects so that only one of the objects are allowed to perform a certain operation at a time.
I got an idea to define an integer that all of the objects have access to.
If the interlock is free, the integer contains a zero. An object is then allowed to put its own unique ID into the integer, perform the operation and put back a zero afterwards.
If any other objects checks the integer while its locked it will see a non-zero value different from its own ID and back off.
It feels almost 100% certain that I'm reinventing an old principle here, but not being CS educated, I don't even know what it's called, so I don't know what to search for.
Is my approach to locking sound/flawed, and what should I be reading up on?
Note that this is fairly low level 61131-3/PLC-programming, so no Singleton patterns etc.
Thanks