帮忙解释一下,他为什么这么写,关于Enterprise Library中Caching Application Block中的部分!!
这是关于添加一个对像到一个缓存中的方式
我对这段代码的疑问是
既然已用了Lock使这段代码成为临界区,为什么用要lockWasSuccessful = Monitor.TryEnter(cacheItemBeforeLock)
请高手解我心中疑问!
CacheItem cacheItemBeforeLock = null;
bool lockWasSuccessful = false;
do
{
lock (inMemoryCache.SyncRoot)
{
if (inMemoryCache.Contains(key) == false)
{
cacheItemBeforeLock = new CacheItem(key, addInProgressFlag, CacheItemPriority.NotRemovable, null);
inMemoryCache[key] = cacheItemBeforeLock;
}
else
{
cacheItemBeforeLock = (CacheItem)inMemoryCache[key];
}
lockWasSuccessful = Monitor.TryEnter(cacheItemBeforeLock);
}
if (lockWasSuccessful == false)
{
Thread.Sleep(0);
}
} while (lockWasSuccessful == false);