Thematic collections in .NET.

What is the standard now when you need a thread-safe collection (e.g. Set). Am I synchronizing it myself or is there a built-in streaming collection?

+62
collections multithreading c #
Jun 05 '10 at 12:17
source share
4 answers

The .NET 4.0 Framework presents several thread-safe collections in System.Collections.Concurrent Namespace :

ConcurrentBag <T>
Represents a thread safe, unordered collection of objects.

ConcurrentDictionary <TKey, TValue>
It is a thread-safe collection of key-value pairs that can be accessed simultaneously by multiple threads.

ConcurrentQueue <T>
It is an assembly of the first stream in the first stream (FIFO) with the stream.

ConcurrentStack <T>
It is an assembly of recent attachments (LIFO) that are not related to the stream.




Other collections in the .NET Framework are not thread safe by default and must be locked for each operation:

lock (mySet) { mySet.Add("Hello World"); } 
+92
Jun 05 '10 at 12:18
source share

Pre.net 4.0 Most .Net collections are not thread safe. You will need to do some work yourself to handle the synchronization: http://msdn.microsoft.com/en-us/library/573ths2x.aspx

Quote from the article:

Collection classes can be made threads safe using any of the following Methods:

Create a thread-safe wrapper using the Synchronized method and access collection only through this wrapper.

If the class does not have a synchronized method, get it from the class and implement the synchronized method using the SyncRoot property.

Use a locking mechanism, such as locking in C # (SyncLock in Visual Basic), on SyncRoot when accessing the collection.

Sync root property
Block Statement

 Object thisLock = new Object(); ...... lock (thisLock) { // Critical code section } 

In .net 4.0 introduced System.Collections.Concurrent namespace

Collection lock
Parallel bag
Parallel order
Parallel dictionary
Hostable Partitioner
Partitioner
Partitioner t

+17
Jun 05 '10 at 12:19
source share

.NET 4 provides a set of thread safe collections at System.Collections.Concurrent

+4
Jun 05 '10 at 12:19
source share

In addition to the very useful classes in System.Collections.Concurrent , one standard method in scripts with most read-rarely-changes (or, if there are frequent, but non-competitive entries) that also apply to .Net Copy-to-Write .

It has several properties that are desirable in highly competitive programs:

  • instances of collection objects themselves are immutable (that is, thread-safe, you can safely list without blocking).
  • it may take as long as it takes, performance and concurrency reads will not be affected
  • can be implemented as a whole to turn any data structure that is not thread safe into

Limitation: if there are parallel recordings, you may have to repeat the changes, therefore, the more simultaneous recordings, the less effective they become. (What an upbeat concurrency at work)

Change Scott Chamberlain's comment reminded me that there is another limitation: if your data structures are huge and changes often occur, then copy-all-to-write can be prohibitive both in terms of memory consumption and processor cost when copying.

+1
Jun 11. '15 at 23:36
source share



All Articles