This project demonstrates how to coordinate communication and synchronization between instances or processes using shared memory and mutexes. It provides a mechanism to control the execution flow and ensure proper handling of resources in multi-instance environments.
Boost C++ Library (interprocess module) Boost Interprocess - Synchronization Mechanisms
The project includes the following components:
The SharedData class represents the shared data structure between instances. It contains the necessary structures and variables for communication and synchronization using shared memory. The class includes the following members:
--> mutex: Mutex to protect access to shared resources.
-->condition: Condition variable to wait when the queue is empty.
-->items: Array to store the items to be filled.
-->message_in: Flag indicating whether there is a message present.
The coordinateInstanceCommunication function facilitates communication and synchronization between instances or processes. It receives two functions as parameters: mainInstance and secondaryInstance, which are executed depending on whether the current instance is the main instance or a secondary instance.
The function performs the following steps:
-->Creates or opens the shared memory object.
-->Sets the size of the shared memory region.
-->Maps the shared memory region.
-->Checks if another instance of the application is already running and tries to lock the mutex.
-->Executes the appropriate logic based on the instance type (main or secondary).
-->Releases the mutex and updates the shared memory object after completing the processing.