I have automated some actions in my app that run on timers set by the user. The user can schedule something to happen at a particular day and time, repeated days, etc. I set a timer to execute the action at the desired time. Let’s call this an automation.
Now the user may be running both my desktop app for local access and my web app for remote access. On the backend, both apps do effectively the same thing - both read the same timer settings to automate the action desired. So both timers will fire and run about the same time. But I don’t want to run the automation twice.
So what I have been doing is the app that first starts to fire its timer sets a mutex. That way the second app tries to access the mutex, it fails and it doesn’t run the automation. Once the first app is done, it releases the mutex after a few seconds or is supposed to. I have seen an instance at a customer where it appears the mutex is somehow not releasing and this user doesn’t run the web app so it should just be the one app creating and releasing the mutex.
So this brings up some questions:
1.) Is this the proper use of a mutex
2.) Is a Mutex the best way to handle this sort of situation
3.) What happens if a Mutex goes out of scope? Does it release? In my case, I have the Mutex as a property of the timer that is executing the automation. That timer in some instances goes out of scope. So does the Mutex then get released?
4.) Any ideas on what could cause the situation where the app creates the mutex but it can’t access it?