We have 2 applications. One written in C# and the other in C++. We need to maintain a counter (in memory) shared between these processes. Every time one of these applications start, it needs to check for this counter and increase it and every time the application shut-down it needs to decrease the counter. If the application has a crash or shut-down using task manager, we also need the counter to decrease.
We thought of using one of the OS synchronization objects like MUTEX.
My question: What kind of sync object is the best for cross process (when one is C# and the other C++)
Hope my question was clear.
Thank you very much,
Adi Barda
You might get away with named semaphore. Semaphore is basically a count, it is here to allow developers limit the number of thread/processes that are accessing some resource. Usually it works like that
WaitForSingleObjector similar and each of them go on without waiting. Each time internal semaphore counter goes down.ReleaseSemaphorefunction. This function increments internal counter of semaphore.I don’t think this is how you want to use it though. So, instead, you should:
WaitForSingleObject(hSemaphore, 0), decreasing the counter. 0 means you don’t want to wait.This is all quite easy.
In C++
In C#
Names and BIG_NUMBERs should be the same obviously.
If this is not sufficient for your task, you will have to look into shared memory and lock access to it though named mutex, but that is a little bit more complicated.