Scrigroup - Documente si articole

Username / Parola inexistente      

Home Documente Upload Resurse Alte limbi doc  


AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows
WordXml

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

The ReaderWriterLock Class

c

+ Font mai mare | - Font mai mic



DOCUMENTE SIMILARE

Trimite pe Messenger
Alarm Clock Specifications soft - script
Arrays, the & operator and function
Arrays - Multidimensional arrays
Structures: Basics of Structures
Constants - Integer constants
A Tutorial Introduction - Getting Started
An example performing input
Control Flow: Statements and Blocks
Input and Output: Standard Input and Output
Character handling - Strings, Untyped pointers


The ReaderWriterLock Class

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;
using System.Threading;

namespace ReadWriteLock


public void ReadInts(ref int a, ref int b)

finally



public void WriteInts(int a, int b)

finally




public class RWApp

private void Write()



private void Read()




An example output from ReadWriteLock could be as follows:

******** Read *********
For i = 0 a = 0 b = 0 ThreadID = 5
******** Read *********
For i = 0 a = 0 b = 0 ThreadID = 4
******** Write ********
x = 10 y = 11 ThreadID = 3
******** Write ********
x = 10 y = 11 ThreadID = 2
For i = 1 a = 10 b = 11 ThreadID = 4
x = 11 y = 12 ThreadID = 3
x = 11 y = 12 ThreadID = 2
For i = 1 a = 11 b = 12 ThreadID = 5
For i = 2 a = 11 b = 12 ThreadID = 4
x = 12 y = 13 ThreadID = 3
x = 12 y = 13 ThreadID = 2
For i = 2 a = 12 b = 13 ThreadID = 5
For i = 3 a = 12 b = 13 ThreadID = 4
x = 13 y = 14 ThreadID = 3
x = 13 y = 14 ThreadID = 2
For i = 3 a = 13 b = 14 ThreadID = 5
For i = 4 a = 13 b = 14 ThreadID = 4
x = 14 y = 15 ThreadID = 3
x = 14 y = 15 ThreadID = 2
For i = 4 a = 14 b = 15 ThreadID = 5

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.

Manual Synchronization

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.

Class

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



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 490
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2022 . All rights reserved

Distribuie URL

Adauga cod HTML in site