C # thread synchronization

Source: Internet
Author: User

Microsoft Official website: https://msdn.microsoft.com/zh-cn/library/ms173179.aspx

The official web site mentions the following:

Lock
Private System.Object LockThis = new System.Object (); Lock (LockThis) {//Place to be protected}

VB version is SyncLock

Monitor
System.Object obj = (System.Object) x; System.Threading.Monitor.Enter (obj); try{dosomething ();} finally{System.Threading.Monitor.Exit (obj);}

Using lock is generally preferable to using the Monitor class directly, on the one hand because lock is more concise and, on the other hand, because lock ensures that the underlying monitor can be released even if the protected code throws an exception.

Event

Like Windows, there are AutoResetEvent and ManualResetEvent, and if it is automatically set, ResetEvent () is called automatically after the event is called WaitForSingleObject () to cause the event to become non-triggering. Waitone,waitany and WaitAll are similar to Waitforsingleobject/waitformultiobject. You can see from the literal meaning that one represents 1, any represents 1 or more, all represents all.

WaitOne causes the thread to wait until a single event becomes signaled, WaitHandle.WaitAny blocks a thread until one or M Ore indicated events become signaled, and WaitHandle.WaitAll blocks the thread until all of the indicated events become SI Gnaled. An event becomes signaled if its Set method is called.

Mutex

Although a mutex can be used for thread synchronization within a process, it is generally preferable to use monitor because the monitor is designed specifically for the. NET Framework, so it can make better use of resources. In contrast, the Mutex class is a wrapper for Win32 constructs. Although a mutex is more powerful than a monitor, it requires an interoperability transformation that consumes computing resources more than the monitors class (mutexes can cross processes and monitor does not).

Interlocked

Similar to MFC's atomic operations (InterlockedIncrement ...), the method of this class allows you to safely increment, decrement, swap, and compare values in any thread.

ReaderWriterLock

Read-write lock, by call: Acquirereaderlock,releasereaderlock,acquirewriterlock,releasewriterlock to complete the reading lock and write lock control

Win32 Other Kernel objects

such as semaphore, can be used to implement producer/consumer mode

SynchronizationAttribute

When we determine that an instance of a class can only be accessed by one thread at a time, we can directly identify the class as synchronization, so that the CLR automatically implements the synchronization mechanism for this class, which in fact involves the concept of a synchronous domain, when the class is designed as follows We can ensure that an instance of a class cannot be accessed concurrently by multiple threads

1) in the declaration of the class, add the System.Runtime.Remoting.Contexts.SynchronizationAttribute property.

2). Inherit to System.ContextBoundObject, must

MethodImplAttribute

If the critical section spans the entire method, that is, the code inside the entire method needs to be locked, it is easier to use the MethodImplAttribute property. So you don't have to lock the inside of the method, just add [MethodImpl (methodimploptions.synchronized)] on the method.


C # thread synchronization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.