I am writing a system for automatically recalculating the results of costly methods, and then storing them in a cache. The cache may be distributed, meaning (obviously) multiple processes can access it.
On the first invocation of the method to cache the result for, I want to spawn a thread that will periodically recalculate the result of the method and update the cache.
I want only a single thread of execution to be spawned per cache item. In a multi-process environment, can this be achieved using a cross process mutex such as this?
http://msdn.microsoft.com/en-us/library/system.threading.mutex.aspx
Are there any pitfalls with this approach?
Edit:
Truth table for my implementation:
// in-cache | mutex-found
// 0 0 //not in cache, no mutex; start thread
// 1 0 //in cache, no mutex; assume thread already started (re-calculating thread could be in another process)
// 0 1 //not in cache, mutex found; thread started, do not start thread - log error (and possibly start new thread)
// 1 1 //in cache, mutex found; thread started, do not start thread