1
- 1
7: Synchronizat ion
Last Modif ied: 6/ 7/ 2004 1:21:28 PM
- 2
Last t ime
Need f or synchronizat ion primit ives Locks and building locks f rom HW
primit ives
Semaphores and building semaphores f rom
locks
- 3
Uses of Semaphores
Mut ual exclusion
Binar y semaphor es (wait / signal used j ust like
lock/ unlock)
“hold”
Managing N copies of a r esour ce
Count ing semaphor es “ent er”
Anyt hing else?
Anot her t ype of synchr onizat ion is t o expr ess
- r der ing/ scheduling const r aint s
“Don’t allow x t o pr oceed unt il af t er y”
- 4
Semaphores f or expressing ordering
I nit ialize semaphore value t o 0 Code:
P
i
P
j
Μ Μ A wait signal B
Execut e B in P
j only af t er A execut ed in P i
Not e: I f signal execut es f irst , wait will
f ind it is an signaled st at e (hist ory!)
- 5
Event s and Signals
Recall: UNI X signals
Kill = send signal; Signal = cat ch signal Many syst em def ined but also signals lef t t o user
def init ion
Can be used f or synchronizat ion
- Signal handler set s a f lag
- Main t hr ead polls on t he value of t he f lag
- Busy wait t hough
Window’s Event s
Synchronizat ion obj ect s used somewhat like semaphores
when t hey are used f or ordering/ scheduling const raint s
One process/ t hread can wait f or an event t o be signaled
by anot her process/ t hread
- 6
Window’s Event s
Cr eat e/ dest r oy
HANDLE CreateEvent( LPSECURITY_ATTRIBUTES lpsa, // security privileges (default = NULL) BOOL bManualReset , // TRUE if event must be reset manually BOOL bInitialState , // TRUE to create event in signaled state LPTSTR lpszEventName ); // name of event (may be NULL) BOOL CloseHandle( hObject );
W ait
DWORD WaitForSingleObject( HANDLE hObject, // object to wait for DWORD dwMilliseconds );
Signal (all t hr eads t hat wait on it r eceive)
BOOL SetEvent ( HANDLE hEvent ); //signal on BOOL ResetEvent( HANDLE hEvent ); //signal off