CATEGORII DOCUMENTE |
Asp | Autocad | C | Dot net | Excel | Fox pro | Html | Java |
Linux | Mathcad | Photoshop | Php | Sql | Visual studio | Windows | Xml |
A ReaderWriterLock defines the lock that implements single-writer and multiple-reader semantics. This class is popularly used in file operations where the file can be read by multiple threads but can be updated by one and only one thread. The four main methods in the ReaderWriterLock class are:
AcquireReaderLock(): This overloaded method acquires a reader lock, using either an integer or a TimeSpan for the timeout value. The timeout can be an invaluable tool used to detect deadlocks.
AcquireWriterLock(): This overloaded method acquires a writer lock, using either an integer or a TimeSpan for the timeout value.
ReleaseReaderLock(): Releases the reader lock.
ReleaseWriterLock(): Releases the writer lock.
Using the ReaderWriterLock class, any number of threads can safely read data concurrently. Only when threads are updating is data locked. Reader threads can acquire a lock only if there are no writers holding the lock. Writer threads can acquire lock only if there are no readers or writers holding the lock.
The following listing, ReadWriteLock.cs, demonstrates the use of the ReaderWriterLock() lock:
using System;An example output from ReadWriteLock could be as follows:
******** Read *********In the above listing, threads wt1 and wt2 are writer threads that acquire writer locks in the WriteInts() method and threads rt1 and rt2 are reader threads that acquire reader locks in the ReadInts() method. In the WriteInts() method, the instance variables x and y are changed to the new values a and b respectively. When thread wt1 or wt2 acquires a writer lock by calling AcquireWriterLock(), no other thread (including the reader threads rt1 and rt2) is allowed access to the object until the thread releases the lock by calling the ReleaseWriterLock() method. This behavior is similar to that of Monitors. In the ReadInts() method, threads rt1 and rt2 acquire reader locks by calling the AcquireReaderLock() method. In the ReadInts() method, both the threads rt1 and rt2 can be given concurrent access to the instance variables x and y. Until the reader threads release their reader locks, neither of the writer threads (wt1 and wt2) is given access to the object. Only reader threads can have concurrent access to the object after acquiring the reader lock.
Monitors might be 'too safe' for threads that plan only to read the data rather than modify it. Monitors also have a performance hit associated with them and, for read-only type access, this performance hit is not necessary. The ReaderWriterLock class offers an elegant solution to dealing with read-and-write access to data by allowing any number of concurrent threads to read the data. It locks the data only when threads are updating the data. Reader threads can acquire a lock if and only if there are no writer threads holding the lock. Writer threads can acquire the lock if and only if there are no reader or writer threads holding the lock. Thus, the ReaderWriterLock behaves in the same way as a critical section. ReaderWriterLock also supports a timeout value that can be very useful in detecting deadlocks.
The third synchronization strategy concerns manual techniques and the .NET Framework provides a classic suite of techniques. They give the programmer the ability to create and manage multithreaded applications using a low-level threading API analogous to the WIN32 Threading API.
The table overleaf shows some of the classes in the System.Threading namespace that can be used for Manual Synchronization.
Description |
|
AutoResetEvent |
The AutoResetEvent class is used to make a thread wait until some event puts it in the signaled state by calling the Set() method. A signaled state indicates that there are no threads waiting. The AutoResetEvent is automatically reset to non-signaled by the system after a single waiting thread has been released. If no threads are waiting, the event object's state remains signaled. The AutoResetEvent corresponds to a Win32 CreateEvent call, specifying false for the bManualReset argument. |
ManualResetEvent |
The Manual ResetEvent class is also used to make a thread wait until some event puts it in the signaled state by calling Set() method. The state of a Manual ResetEvent object remains signaled until it is set explicitly to the non-signaled state by the Reset() method. The ManualResetEvent corresponds to a Win32 CreateEvent call, specifying true for the bManualReset argument. |
Mutex |
A Mutex lock provides cross-process as well as cross-thread synchronization. The state of the Mutex is signaled if no thread owns it. The Mutex class doesn't have all of the wait-and-pulse functionality of the Monitor class, but it does offer the creation of named mutexes (using the overloaded constructor) that can be used between processes. The benefit of using a Mutex over a Monitor is that a Mutex can be used across processes whereas a Monitor cannot. |
Interlocked |
The Interlocked class provides methods for atomic, non-blocking integer updates that are shared between multiple threads. The threads of different processes can use this mechanism if the variable is in shared memory. |
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 777
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved