Condition variable module. Provides procedures for thread
synchronization using condition variables paired with mutexes.
Requires --m2plus and links with pthreads.
TYPE Condition; (* opaque condition variable handle *)
PROCEDURE Create(): Condition;
PROCEDURE Wait(c: Condition; m: Mutex);
PROCEDURE Signal(c: Condition);
PROCEDURE Broadcast(c: Condition);
PROCEDURE Destroy(c: Condition);
Waitatomically releases the mutexmand blocks until the condition is signaled, then reacquiresmbefore returning.Signalwakes one thread waiting on the condition.Broadcastwakes all threads waiting on the condition.- Always check the wait predicate in a loop, as spurious wakeups are possible.
MODULE CondDemo;
FROM Mutex IMPORT Mutex, Create, Lock, Unlock;
FROM Condition IMPORT Condition, Wait, Signal;
VAR
mu: Mutex;
cv: Condition;
ready: BOOLEAN;
PROCEDURE Producer;
BEGIN
Lock(mu);
ready := TRUE;
Signal(cv);
Unlock(mu);
END Producer;
PROCEDURE Consumer;
BEGIN
Lock(mu);
WHILE NOT ready DO Wait(cv, mu) END;
Unlock(mu);
END Consumer;
END CondDemo.