I am developing a C ++ class (MyClass.cpp) that I will compile as a dynamic shared library (MyClass.so).
This shared library will be used by two different applications running on the same Linux machine.
They are two different applications. It is not a multithreaded application, nor can it be.
In this class there is a MyClass::WriteHardware method that must be executed with mutual exclusion, so it is impossible for the two applications to run it at the same time.
What is the correct and simplest way to achieve this synchronization between two different processes, considering that the MyClass.so library is common to both?
Would a mutex work in this case, since it is not a multithreaded application?
> In this class there is a MyClass::WriteHardware method that must be executed with mutual exclusion
Just write?
Or is that just the tip of the iceberg, and there are many other contention points within the same class over the hardware?
How will MyClass.so initialise itself on first use?
You need to make sure you always end up with exactly one instance of your hardware, protected by whatever mechanism you choose, regardless of how close in time both processes are started.
What happens if one process just dies (or is killed) while it has some kind of 'lock'?
You need to use a synchronization method from the OS, such as a semaphore or file lock as a building for synchronisation.
An alternative, is to manage the resource with a single program, say a daemon, then have the two programs talk to that daemon; client-server. The programs, then, don't even need to be on the same computer.