. Net framework 4 thread security overview,. netframework
Thread security:
If multiple threads are running simultaneously in the process where your code is located, these threads may run the code at the same time. If the result of each running is the same as that of a single thread, and the value of other variables is the same as expected, it is thread-safe.
In the early days, Microsoft's collection class implemented thread security through the most basic locking. We can note that there are two methods and fields in the system. collections assembly that provide the locking mechanism, namely the _ syncRoot field and the Synchronized method. However, locking is coarse-grained for synchronization. The set provides thread security through the Synchronized attribute. The operating principle of the package is: to lock the entire set for each add or delete operation. Therefore, every thread trying to access the collection must wait until it is its turn to obtain the lock. For large sets, performance will be significantly reduced
. Net framework 4 provides fine-grained locking and lock-free mechanisms. The thread security set in the new system. collections. concurrent Assembly uses lightweight synchronization mechanisms, such as SpinLock, SpinWait, SemaphoreSlim, and CountdownEvent.
Lightweight synchronization primitives can only be used in one process. The heavyweight versions support cross-process synchronization.
Lightweight synchronization saves CPU overhead and avoids context switching
Thread Security Implementation of ConcurrentQueue:
Thread Security Implementation of ConcurrentDictionary:
ConcurretnStack thread security implementation:
Producer and consumer issues are typical multi-threaded applications:
A simple expression of this problem is: one or more threads (producer threads) generate some data, and one or more threads (Consumer threads) you need to extract the data and perform some corresponding work.
Microsoft's. net framework4 provides concurrent classes specifically designed to solve such problems: BlockingCollection and IProducerConsumerCollection.
BlockingCollection is a collection with blocking functions. It completes the Algorithm functions of classic producer consumers. Instead of implementing the underlying storage structure, it uses several sets that implement the IProducerConsumerCollection interface as the underlying data structure, such as ConcurrentBag, ConcurrentStack or ConcurrentQueue. You can input this parameter when constructing a BlockingCollection instance. If this parameter is not specified, ConcurrentQueue is used as the storage structure by default.
I have to say that FCL is perfect now. Remember to read a blog and complain about it. net's thread security is not as good as java. At that time, I was actually a little sorry, but how can I do well with so many technical talents from Microsoft. Check whether FCL is good.
Thread security set
Https://msdn.microsoft.com/zh-cn/library/ms228964 (v = vs.110). aspx
Thread security set Overview
Https://msdn.microsoft.com/zh-cn/library/dd997305%28v=vs.110%29.aspx? Lc = 1, 2052
C #4.0 thread security
Http://www.cnblogs.com/chengxiaohui/articles/5672768.html