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 Wait() and Pulse() Mechanism

c



+ Font mai mare | - Font mai mic



The Wait() and Pulse() Mechanism

The Wait() and Pulse() mechanism is used for interaction between threads. When a Wait() method is issued on an object, the thread that is accessing that object waits until it gets a signal to wake up. The Pulse() and PulseAll() are used for signaling to waiting thread(s). The following listing is an example of how the Wait() and Pulse() methods work, WaitAndPulse.cs:



The Wait() and Pulse() methods can be called only within the Enter() and Exit() code block.

using System;
using System.Threading;

namespace WaitAndPulse


public class WaitPulse1

public WaitPulse1(LockMe l)


public void CriticalSection()


Console.WriteLine('WaitPulse1:Exiting Thread ' +
Thread.CurrentThread.GetHashCode());

//Exit the Critical Section
Monitor.Exit(this. lM);



public class WaitPulse2


public WaitPulse2(LockMe 1)


public void CriticalSection()


Console.WriteLine('WaitPulse2:Exiting Thread ' +
Thread.CurrentThread.GetHashCode());
//Exit the Critical Section
Monitor.Exit(this. lM);



public class ClassForMain




The output from WaitAndPulse is:

WaitPulse1:Entered Thread 2
WaitPulse2:Entered Thread 3
WaitPulse2:Result = 0 ThreadID 3
WaitPulse1:WokeUp
WaitPulse1:Result = 0 ThreadID 2
WaitPulse2:WokeUp
WaitPulse2:Result = 1 ThreadID 3
WaitPulse1:WokeUp
WaitPulse1:Result = 1 ThreadID 2
WaitPulse2:WokeUp
WaitPulse2:Result = 2 ThreadID 3
WaitPulse1:WokeUp
WaitPulse1:Result = 2 ThreadID 2
WaitPulse2:WokeUp
WaitPulse2:Result = 3 ThreadID 3
WaitPulse1:WokeUp
WaitPulse1:Result = 3 ThreadID 2
WaitPulse2:WokeUp
WaitPulse2:Result = 4 ThreadID 3
WaitPulse1:WokeUp
WaitPulse1:Result = 4 ThreadID 2
WaitPulse1:Exiting Thread 2
WaitPulse2:WokeUp
WaitPulse2:Exiting Thread 3
WaitPulse1: Exiting Thread 2
WaitPulse2: WokeUp
WaitPulse2: Exiting Thread 3

In the Main method, we create a LockMe object called . Then we create two objects of type WaitPulse1 and WaitPulse2, and pass them as delegates so that the threads can call the CriticalSection() method of both the objects. Note that the LockMe object instance in WaitPulse1 is the same as the LockMe object instance in WaitPulse2, as the object has been passed by reference to their respective constructors. After initializing the objects, we create two threads, t1 and t2, and pass them the two CriticalSection() methods respectively.

Assuming that WaitPulse1.CriticalSection() gets called first, then thread t1 enters the critical section of the method with a lock on the LockMe object and then executes Monitor.Wait() in the for loop. By executing Monitor.Wait(), it is waiting for a runtime notification (Monitor.Pulse()) from another thread to be woken up. We lock the LockMe object because we want only one thread to access the shared LockMe instance at any point of time.

Note that when the thread executes the Monitor.Wait() method, it releases the lock on the LockMe object temporarily, so that other threads can access it. After thread t1 goes into the waiting state, thread t2 is free to access the LockMe object. Even though the LockMe object is a separate object (WaitPulse1 and WaitPulse2), they both refer to the same object reference. Thread t2 acquires the lock on the LockMe object and enters the WaitPulse2.CriticalSection() method. As soon as it enters the for loop, it sends a run-time notification (Monitor.Pulse()) to the thread that is waiting on the LockMe object (t1 in this case) and goes off to the waiting state. As a result, t1 wakes up and acquires the lock on the LockMe object. Thread t1 then accesses the result variable and sends a run-time notification to the thread waiting on the LockMe object (thread t2 in this case). This cycle continues until the for loop ends.

If you compare the description above with the output of the program, the concept will be crystal clear. It is important to note that every Enter() method should be accompanied by an Exit() method or else the program will never quit.

The Enter() method takes an object as a parameter. If the object parameter is null, a method variable, or an object of a value type like an integer an exception will be thrown.



Politica de confidentialitate | Termeni si conditii de utilizare



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 711
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