sematree (a semaphore like utility) is used to manipulate locks
that persist across process lifetimes.

You can choose either an implementation in C or in shell.
They use different methods to achieve the locking for illustration.
The shell uses the atomicity of directory creation, whereas the C
version uses open(..O_EXCL..). Note the C version also supports
millisecond resolution but this could be added (less efficiently)
to the shell version easily by using usleep, if required.

Here is some more information and common usage examples