Publié : ven. 19/sept./2008 20:56
Un exemple de Freak montrant comment utiliser les fonctions semaphore.
Freak a écrit : A semaphore is a synchronisation object with a counter.
SignalSemaphore() increases that count, WaitSemaphore() decreases it.
If WaitSemaphore() would cause the count to drop below 0, it will block and wait until another SignalSemaphore() call is made.
Here is an example, where a semaphore is used to protect a queue from running out of elements:
Code : Tout sélectionner
Global NewList Queue.l()
Global *Semaphore = CreateSemaphore(0) ; to guard against an empty queue
Global *Mutex = CreateMutex() ; there is still need for a normal mutex to sync list commands
Procedure ProducerThread(Dummy)
For i = 1 To 100
Debug "Enqueueing: " + Str(i)
; add a new element at the queue end
LockMutex(*Mutex)
LastElement(Queue())
AddElement(Queue())
Queue() = i
UnlockMutex(*Mutex)
; signal that an element was added (increase semaphore count)
SignalSemaphore(*Semaphore)
; simulate some work
Delay(Random(200))
Next i
EndProcedure
Procedure ConsumerThread(Dummy)
For i = 1 To 100
; Wait for something to be in the queue.
; If count > 0, decreases count and returns immediately
; If count = 0, wait for another SignalSemaphore()
WaitSemaphore(*Semaphore)
; Read the head element from the queue
LockMutex(*Mutex)
FirstElement(Queue())
x = Queue()
DeleteElement(Queue())
UnlockMutex(*Mutex)
Debug "Dequeued: " + Str(i)
; simulate some work
Delay(Random(200))
Next i
EndProcedure
*Consumer = CreateThread(@ConsumerThread(), 0)
*Producer = CreateThread(@ProducerThread(), 0)
WaitThread(*Consumer)
WaitThread(*Producer)
End