CATEGORII DOCUMENTE |
Asp | Autocad | C | Dot net | Excel | Fox pro | Html | Java |
Linux | Mathcad | Photoshop | Php | Sql | Visual studio | Windows | Xml |
The .NET Framework provides a few classes in the System.Threading, System.EnterpriseServices, and System.Runtime.Compiler namespaces that allow the programmer to develop thread-safe code. The table below briefly describes some of the synchronization classes in the .NET Framework.
Class |
Description |
Monitor |
Monitor objects are used to lock the critical sections of code so that one and only one thread has access to those critical sections at any point of time. They help ensure the atomicity of critical sections of code. |
Mutex |
Mutex objects are similar to Monitor objects with the exception that they grant exclusive access to a resource shared across processes to only one thread. The Mutex overloaded constructor can be used to specify Mutex ownership and name. |
AutoResetEvent, ManualResetEvent |
AutoResetEvent and ManualResetEvent are used to notify one or more waiting threads that an event has occurred. Both these classes are NotInheritable. |
Interlocked |
The Interlocked class has the CompareExchange(), Decrement(), Exchange(), and Increment() methods that provide a simple mechanism for synchronizing access to a variable that is shared by multiple threads. |
SynchronizationAttribute |
SynchronizationAttribute ensures that only one thread at a time can access an object. This synchronization process is automatic and does not need any kind of explicit locking of critical sections. |
MethodImplAttribute |
This attribute notifies the compiler on how the method should be implemented. |
The System.Runtime.CompilerServices namespace, as its name suggests, contains attributes that affect the runtime behaviour of the CLR (Common Language Runtime). MethodImplAttribute is one such attribute that notifies the CLR on how the method is implemented. One of the MethodImplAttribute constructors accepts the MethodImplOptions enumeration as a parameter. The MethodImplOptions enumeration has a field named Synchronized that specifies that only one thread is allowed to access this method at any point of time. This is similar to the lock keyword that we used in the previous example. The listing below of MI.cs shows how you can use this attribute to synchronize a method:
using System;The output from the above listing will be similar to the following (output might vary from computer to computer as Thread IDs might differ):
In
the above listing, the MI
class has two methods: doSomeWorkSync()
and doSomeWorkNoSync().
The MethodImpl
attribute has been applied to the doSomeWorkSync() method to synchronize it,
whereas doSomeWorkNoSync()
is kept as it is so that multiple threads can access the method at the same
time. In the
If you look at the output carefully, you will find that thread 2 (t1) and thread 3 (t2) entered the method doSomeWorkNoSync() at the same time, whereas, once thread 4 (t3) acquired the lock on the method doSomeWorkSync(), thread 5 (t4) was not allowed to enter the method until thread 4 (t3) released the lock on that method.
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 639
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2025 . All rights reserved