Presentation is loading. Please wait.

Presentation is loading. Please wait.

ISP – 7 th Recitation Mid semester!!! Semaphores – reminder Events Code examples.

Similar presentations


Presentation on theme: "ISP – 7 th Recitation Mid semester!!! Semaphores – reminder Events Code examples."— Presentation transcript:

1 ISP – 7 th Recitation Mid semester!!! Semaphores – reminder Events Code examples

2 Semaphores - Reminder A semaphore object is a synchronization object that maintains a count between zero and a specified maximum value. The count is decremented each time a thread completes a wait for the semaphore object and incremented each time a thread releases the semaphore. When the count reaches zero, no more threads can successfully wait for the semaphore object state to become signaled. The state of a semaphore is set to signaled when its count is greater than zero, and nonsignaled when its count is zero.

3 Semaphores - Reminder CreateSemaphore() creates a new semaphore with a given name, max counter value and an initial value. OpenSemaphore() returns a handle to an existing semaphore given its name. WaitForSingleObject() decrements the counter and returns as long as the counter>0. ReleaseSemaphore() increases the counter by a given value.

4 Events An event object is a synchronization object whose state can be explicitly set to signaled by use of the SetEvent function. There are two types of events: 1.Manual-Reset Event - An event object whose state remains signaled until it is explicitly reset to nonsignaled by the ResetEvent function. While it is signaled, any number of waiting threads, or threads that subsequently specify the same event object in one of the wait functions, can be released. 2.Auto-Reset Event – An event object whose state remains signaled until a single waiting thread is released, at which time the system automatically sets the state to nonsignaled. Note: Auto-Reset mode is less interesting as it behaves like a mutex.

5 Creating an Event Creating a new event Syntax : HANDLE CreateEvent( SECURITY_ATTRIBUTES *EventAttributes, BOOL ManualReset, BOOL InitialState, char* Name ); ManualReset determines if the event is of manual-reset type (TRUE) or automatic-reset type (FALSE). InitialState determines if the newly created event is first in singaled state (TRUE) or nonsignaled (FALSE). Name field identifies the event in the system. If name already exists, a handle to the existing event is returned and GetLastError() shows ERROR_ALREADY_EXISTS.

6 Opening an Existing Event Opening an existing event Syntax : HANDLE OpenEvent( DWORD DesiredAccess, BOOL InheritHandle, char* Name ); DesiredAccess requires a minimum permission of SYNCHRONIZE.

7 Making an event “signaled” Setting the event state to “signaled” Syntax: BOOL SetEvent(HANDLE hEvent); hEvent is the handle to the event. Return value is non zero if successful and zero if not.

8 Making an event “nonsignaled” Setting a manual-reset event state to “nonsignaled” Syntax: BOOL ResetEvent(HANDLE hEvent); hEvent is the handle to the event. Return value is non zero if successful and zero if not.

9 WaitForSingleObject() – Events (For the last time!) For events, the meaning to the states is up to us. In automatic-reset mode, the event type is set to nonsignaled after the first thread returns from a wait function. In manual-reset mode, all waiting threads return as long as the event is set to signaled.


Download ppt "ISP – 7 th Recitation Mid semester!!! Semaphores – reminder Events Code examples."

Similar presentations


Ads by Google