for (int i = 0; i < 100; i++)
{
// If current thread needs resource(i) then
Mutex mutex = new Mutex(false, "Mutex" + i.ToString());
mutex.WaitOne();
// synchronized access to resource(i)
mutex.ReleaseMutex();
}
我們有100個資源,每個人都應該由單個線程併發訪問同步訪問命名mutexs(這是確定訪問資源[2]和資源[5]同時),所以我使用了上面的代碼。在這種情況下,命名互斥體的最佳選擇是什麼?最佳替代對每個資源
實際上資源沒有索引。他們有獨特的名字,所以有'resource [key]'而不是'resource(i)',那麼什麼類型適合用作鎖? – Xaqron 2011-04-20 23:42:29
我會建議使用我發佈的解決方案,並使用'List