Hashtable to Dictionary<> syncroot .

前端 未结 4 1175
闹比i
闹比i 2020-12-03 14:45

Hashtables have a syncroot property but generic dictionaries don\'t. If I have code that does this:

lock (hashtable.Syncroot)
{
....
}

How

4条回答
  •  甜味超标
    2020-12-03 15:45

    The new thinking behind SyncRoot is that it was a mistake in the original design. If the only thing to lock is the dictionary and it's private, you can lock it or another object that serves as the synchronization object. The latter technique is useful when the state you are protecting is more than just the dictionary.

    // used as you would have used SyncRoot before
    object _syncLock = new object();
    Dictionary numberMapper = new Dictionary();
    
    // in some method...
    lock (_syncLock)
    {
        // use the dictionary here.
    }
    

提交回复
热议问题