usingSystem.Collections.Generic;usingSystem.Threading;namespacecsharputilhelpv2{// <summary> /// for dictionary thread-safe Operations tool classes based on. NET 2.0 /// description /// Default read lock timeout 1000 milliseconds /// Default write lock timeout 1000 milliseconds // . NET 4.0 can be implemented using Concurrentdictionary. // </summary> /// <typeparam name= "T" > Generics </typeparam> Public classthreadsafedictionaryv2<t> {/ * Reference* 1.* Synchronization control with Monitor or mutex: this is always inefficient because the exclusive access model does not allow any form of concurrent access. * Many times, applications have read operations while accessing resources, with relatively few write operations. To address this problem,* C # provides a System.Threading.ReaderWriterLock class to accommodate multi-user read/single-user write scenarios. This class enables the following functions:* If the resource is not locked by a write operation, then any thread can lock the resource for read operations, and there is no limit to the number of read locks, that is, multiple threads can lock the resource at the same time to read the data. * If a resource has not been added to any read or write operation locks, then one and only one thread can add write locks to the resource to write to the data. The simple saying is: Read operation lock is a shared lock, allowing multiple threads to read data simultaneously;* Write Operation Lock is an exclusive lock, at the same time, only one thread is allowed to write. * Citation Link: http://www.csharpwin.com/dotnetspace/12761r5814.shtml * * 2.* ReaderWriterLock is used to synchronize access to resources. At any given time, it allows multiple threads to read access at the same time, or allow a single thread to write access. * ReaderWriterLock provides more throughput than a simple one-thread lock (for example, Monitor) at a time when resources are not constantly changing. * The performance of ReaderWriterLock is best when most accesses are read access, and the write access frequency is low and the duration is relatively short. * Multiple read threads operate alternately with a single write thread, so neither the read thread nor the write thread will block for a long time. * Most methods of acquiring locks on ReaderWriterLock use a timeout value. Use timeouts to avoid deadlocks in your application. * If you do not use timeouts, these two threads will have a deadlock. * Citation Link: http://msdn.microsoft.com/zh-cn/library/system.threading.readerwriterlock (v=vs.80). aspx * * 3. Other links* Http://tinythreadsafecache.codeplex.com/SourceControl/latest#TinyThreadSafeCache.cs* http://www.grumpydev.com/2010/02/25/thread-safe-dictionarytkeytvalue/* http://stackoverflow.com/questions/157933/whats-the-best-way-of-implementing-a-thread-safe-dictionary * http://stackoverflow.com/questions/15095817/ Adding-to-a-generic-dictionary-causes-indexoutofrangeexception */ReaderWriterLock Rwlock =NewReaderWriterLock ();//Declare read-write lock. NET 3.5+ recommended ReaderWriterLockSlimdictionary<string, t> dic =Newdictionary<string, t> ();//dictionary Static intreadertimeout = 1000;//default read lock timeout 1000 ms Static intwritertimeout = 1000;//Default write lock timeout 1000 ms // <summary> /// default constructor // </summary> PublicThreadSafeDictionaryV2 () {}// <summary> /// with parameter constructor // </summary> // <param name= "_readertimeout" > Read lock timeout setting "unit milliseconds" </param> // <param name= "_writertimeout" > Write lock Timeout setting "unit milliseconds" </param> PublicThreadSafeDictionaryV2 (int_readertimeout,int_writertimeout) {readertimeout = _readertimeout; Writertimeout = _writertimeout; }// <summary> //This "thread safe" // </summary> // <param name= "key" > Keys </param> /// <returns> value </returns> PublicT This[stringKey] {get {rwlock. AcquireReaderLock (readertimeout);Try{returnDic[key]; }finally{Rwlock. Releasereaderlock (); }} set {Rwlock. AcquireWriterLock (writertimeout);Try{Dic[key] =value; }finally{Rwlock. Releasewriterlock (); } } }// <summary> // ADD "thread safe" /// default timeout 1000 milliseconds // </summary> // <param name= "key" > Keys </param> // <param name= "val" > Value </param> Public voidADD (stringKey, T val) {Add (Key, Val, writertimeout); }// <summary> // ADD "thread safe" // </summary> // <param name= "key" > Keys </param> // <param name= "val" > Value </param> // <param name= "Timeout" > timeout setting "milliseconds" </param> Public voidADD (stringKey, T Val,intTimeout) {Rwlock. AcquireWriterLock (timeout);Try{Dic[key] = val; }finally{Rwlock. Releasewriterlock (); } }// <summary> // Get "thread safe" /// default timeout 1000 milliseconds // </summary> // <param name= "key" > Keys </param> /// <returns> value </returns> PublicT Get (stringKey) {returnGet (key, readertimeout); }// <summary> // Get "thread safe" // </summary> // <param name= "key" > Keys </param> // <param name= "Timeout" > timeout setting "milliseconds" </param> /// <returns> value </returns> PublicT Get (stringKeyintTimeout) {Rwlock. AcquireReaderLock (timeout);Try{T val; Dic. TryGetValue (Key, outVal);returnVal }finally{Rwlock. Releasereaderlock (); } }// <summary> // Remove "thread safe" /// default timeout 1000 milliseconds // </summary> // <param name= "key" > Keys </param> Public voidRemove (stringKey) {Remove (key, writertimeout); }// <summary> // Remove "thread safe" // </summary> // <param name= "key" > Keys </param> // <param name= "Timeout" > timeout setting "milliseconds" </param> Public voidRemove (stringKeyintTimeout) {Rwlock. AcquireWriterLock (timeout);Try{dic. Remove (key); }finally{Rwlock. Releasewriterlock (); } }// <summary> // Clear "thread safe" /// default timeout 1000 milliseconds // </summary> Public voidClear () {clear (writertimeout); }// <summary> // Clear "thread safe" // </summary> // <param name= "Timeout" > timeout setting "milliseconds" </param> Public voidClear (intTimeout) {Rwlock. AcquireWriterLock (timeout);Try{dic. Clear (); }finally{Rwlock. Releasewriterlock (); } }// <summary> // ContainsKey "thread safe" /// default timeout 1000 milliseconds // </summary> // <param name= "key" > Keys </param> // <returns> includes </returns> Public BOOLContainsKey (stringKey) {returnContainsKey (key, readertimeout); }// <summary> // ContainsKey "thread safe" // </summary> // <param name= "key" > Keys </param> // <param name= "Timeout" > timeout setting "milliseconds" </param> // <returns> includes </returns> Public BOOLContainsKey (stringKeyintTimeout) {Rwlock. AcquireReaderLock (timeout);Try{returnDic. ContainsKey (key); }finally{Rwlock. Releasereaderlock (); } }// <summary> // Count "thread safe" /// default timeout 1000 milliseconds // </summary> // <returns></returns> Public intCount () {returnCount (readertimeout); }// <summary> // Count "thread safe" // </summary> // <param name= "Timeout" > timeout setting "milliseconds" </param> // <returns>Count</returns> Public intCount (intTimeout) {Rwlock. AcquireReaderLock (timeout);Try{returnDic. Count; }finally{Rwlock. Releasereaderlock (); } } }}