Scrigroup - Documente si articole

     

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

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

The Mutex Class

c



+ Font mai mare | - Font mai mic



The Mutex Class

Like the ManualResetEvent and the AutoResetEvent classes, the Mutex class is also derived from the WaitHandle class. It is very similar to the Monitor class with the exception that it can be used for interprocess synchronization. Let's look at an example, WroxMutex.cs:



using System;
using System.Threading;

namespace WroxMutex


public void Run()



The output from WroxMutex is:

Thread will sleep for 5 seconds
In Run method
Thread Woke Up
Thread will sleep for 10 seconds
Before WaitOne
End of Run method
Lock owned by Main Thread

In WroxMutex, we construct a Mutex with a Boolean value indicating that the calling thread should have initial ownership of the Mutex, and a string that is the name of the Mutex. We then create a thread, which calls the Run() method. The Mutex is still owned by the main thread. In the Run() method, the thread t has to wait until the main thread releases the ownership of the Mutex. Thus, the thread t waits at the WaitOne() method call in the Run() method. After sleeping for five seconds, the main thread releases the Mutex lock. Thread t then gets the ownership of the Mutex lock and then goes off for a sleep. Now, the Main method will not be able to acquire the ownership of the Mutex until the thread t releases the ownership or aborts. In this case, thread t times out and dies, so the ownership of the Mutex is transferred back to the main thread.



Politica de confidentialitate | Termeni si conditii de utilizare



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 615
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 2024 . All rights reserved