我正在编写一个系统,用于自动重新计算昂贵方法的结果,然后将它们存储在缓存中。缓存可能是分布式的,这意味着(显然)多个进程可以访问它。
在第一次调用缓存结果的方法时,我希望生成一个线程,该线程将定期重新计算方法的结果并更新缓存。
我只希望每个缓存项生成一个执行线程。在多进程环境中,是否可以使用这样的跨进程互斥来实现这一点?
http://msdn.microsoft.com/en-us/library/system.threading.mutex.aspx
编辑:
我的实现的真值表:
// 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