This article is a mirror article of machine translation, please click here to jump to the original article.

View: 69126|Reply: 4

[Source] .NET/C# Thread Safety Collection (collation)

[Copy link]
Posted on 6/6/2017 5:13:53 PM | | |
Collections introduced in the .NET Framework 1.0 can be found in the System.Collections namespace. These collections, including the commonly used ArrayList and Hashtable, provide some kind of thread safety through the Synchronized property, which returns a thread-safe wrapper related to the collection. The wrapper works by locking the entire set for each add or remove operation. Therefore, each thread trying to access the collection must wait until it is its turn to acquire the lock. This is not scalable and will result in a significant performance degradation for large collections. In addition, this design does not completely prevent contention. For more information, see the following page on the MSDN website: Synchronization in Generic Collections

The collection class introduced in the .NET Framework 2.0 can be found in the System.Collections.Generic namespace. These collection classes include List<T>, Dictionary< TKey, TValue> etc. These classes provide higher type security and performance compared to .NET Framework 1.0 classes. However, the .NET Framework 2.0 collection class does not provide any thread synchronization; When adding or removing items on multiple threads at the same time, the user code must provide all synchronization.

We recommend that you use concurrent collection classes in .NET Framework 4 because they provide not only the type safety of the .NET Framework 2.0 collection classes, but also more efficient and complete thread security than the thread security provided by the .NET Framework 1.0 collection.


Some concurrent collection types use lightweight synchronization mechanisms such as SpinLock, SpinWait, SemaphoreSlim, and CountdownEvent, which are new in the .NET Framework 4. Typically, the above synchronization types use "busy spinning" for a short period of time before putting the thread into the actual waiting state. If the wait time is expected to be very short, the spin will consume much less compute resources than the wait, which involves kernel conversions that will consume a lot of resources. For collection classes that use rotation, this efficiency means that multiple threads are able to add and remove items at a very fast rate. For more information on limiting and blocking, see SpinLock and SpinWait. The ConcurrentQueue<T> and ConcurrentStack<T> classes don't use locks at all. Instead, they rely on Interlocked operations for thread safety.

illustrate

Because concurrent collection classes support ICollection, they provide implementations for the IsSynchronized and SyncRoot properties, even if they are unrelated. IsSynchronized always returns false, while SyncRoot is always null (Nothing in Visual Basic).

The following table lists the collection types in the System.Collections.Concurrent namespace.



type
description
BlockingCollection<T>
Provides<T> any type of throttling and blocking for implementing IProducerConsumerCollection. For more information, see BlockingCollection overview.
ConcurrentDictionary<TKey,&#8194;TValue>
Key/value is thread-safe implementation of the dictionary.
ConcurrentQueue<T>
Thread safety implementation of FIFO (first in, first out) queues.
ConcurrentStack<T>
Thread-safe implementation of LIFO (last-in, first-out) stacks.
ConcurrentBag<T>
Thread-safe implementation of an out-of-order collection of elements.
IProducerConsumerCollection<T>
types must be implemented in BlockingCollectioninterfaces used in the







Previous:.net/c# itextsharp to generate PDF code
Next:Install-Package: "MvvmLightLibs" already has a dependency defined for "CommonServiceLocator"...
Posted on 11/8/2017 3:24:45 PM |
Yes, I learned
 Landlord| Posted on 2/8/2021 10:00:41 AM |
List is<T> thread-safe if the list is not modified at all during the iteration.
 Landlord| Posted on 3/23/2021 11:32:46 AM |
ConcurrentDictionary supports new and updated updates
https://www.itsvse.com/thread-2955-1-1.html

 Landlord| Posted on 6/24/2023 2:20:57 PM |
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com