Synchronizing data for multithreaded processing
https://msdn.microsoft.com/zh-cn/library/z8chs7ft (vs.80). aspx
. NET Framework 2.0
When multiple threads can invoke properties and methods of a single object, it is important to synchronize those calls. Otherwise, one thread may break the task that another thread is performing, leaving the object in an invalid state. Classes whose members are not affected by such interrupts are called thread-safe classes.
The common Language infrastructure provides several policies that you can use to synchronize access to instances and static members:
Synchronize code regions. You can improve performance by using the Monitor class or the compiler support for that class to synchronize only code blocks that require that class.
Manual synchronization. You can use the synchronization objects provided by the. NET Framework class Library. See Overview of Synchronization Primitives, which discusses the Monitor class.
The synchronization context. You can use SynchronizationAttribute to enable simple automatic synchronization for ContextBoundObject objects.
The Synchronized property. Several classes, such as Hashtable and Queue, provide a Synchronized property that can return a thread-safe wrapper for an instance of the class. See Collections and synchronization (thread safety).
The common language runtime provides a threading model in which classes are grouped into categories that can be synchronized in a variety of ways, as required. The following table shows the synchronization support provided for fields and methods that have a given synchronization category.
category |
Global Fields |
static fields |
Static Methods |
instance Fields |
instance Method |
Specific code blocks |
No synchronization |
Whether |
Whether |
Whether |
Whether |
Whether |
Whether |
Synchronization context |
Whether |
Whether |
Whether |
Is |
Is |
Whether |
Synchronizing Code regions |
Whether |
Whether |
Only when tagged |
Whether |
Only when tagged |
Only when tagged |
Manual Sync |
Manual |
Manual |
Manual |
Manual |
Manual |
Manual |
No synchronization
This is the default for objects. Any thread can access any method or field at any time. Only one thread can access these objects at a time.
Manual Sync
The. NET Framework Class Library provides a number of classes for synchronizing threads. See Overview of Synchronization Primitives.
Synchronizing Code regions
You can use the Monitor class or the compiler keyword to synchronize code blocks, instance methods, and static methods. Synchronizing static fields is not supported.
Both Visual Basic and C # support the use of specific language keywords to tag blocks of code, using the lock statement in C #, and the SyncLock statement in Visual Basic. When the code is executed by a thread, an attempt is made to acquire the lock. If the lock is already fetched by another thread, the thread is always in a forbidden state until the lock becomes available. When a thread exits the synchronization code block, the lock is freed, regardless of how the thread exits.
Attention |
The lock and SyncLock statements are using System.Threading.Monitor.Enter (System.Object) and System.Threading.Monitor.Exit (System.Object), so that they can be used with other methods of Monitor in the synchronization area. |
You can also use the methodimplattribute and methodimploptions.synchronized retouching methods, their effects and using Monitor Or one of the compiler keywords locks the entire method body the same.
Thread.Interrupt can be used to cause a thread to jump out of a blocking operation, such as waiting for access to the synchronization code region. Thread.Interrupt is also used to cause threads to jump out of thread.sleep.
Points |
To protect the static method ( Shared method in Visual Basic), do not lock the type, that is: typeof (MyType)in C #, Ge in Visual Basic Ttype (MyType) or Mytype::typeidin C + +. Instead, use a private static object instead. Similarly, do not use this in C # ( Mein Visual Basic) to lock an instance method. Instead, use a private object. Classes or instances can be locked by other code, and locking your own code can cause deadlocks or performance problems. |
Compiler support
Both Visual Basic and C # support the use of Monitor.Enter and monitor.exit to lock language keywords for objects. Visual Basic supports SyncLock statements; C # supports lock statements.
In both cases, if an exception is thrown in the code block, the lock acquired by the lock or SyncLock lock is automatically freed. C # and the Visual Basic compiler use monitor.enterat the start of a try when issuing a try/finally block, at the finally Use Monitor.Exitin the block. If an exception is thrown inside the lock or SyncLock block, the finally handler is run, allowing you to perform any cleanup work.
Synchronization context
You can use any contextboundobject synchronizationattribute To synchronize all instance methods and fields. All objects in the same context domain share the same lock. Allows multiple threads to access methods and fields, but only one thread is allowed to access at any one time.
Synchronizing data for multithreading (including instances and static members)