start all threads waiting on a condition variable

Parameters

 condId - (integer) condition variable identifier

Description

 • The Broadcast command wakes up all the threads waiting on the condition variable identified by condId.
 • The Broadcast command does not require the calling thread to hold a lock on the mutex that was used when the waiting threads called Wait.  However, holding the lock while calling Broadcast allows for a greater level of control over the scheduling of the signaled thread.
 • If no threads are waiting on the condition variable, then calling Broadcast has no effect.
 • For more information on using condition variables, see the Condition Variable help page.

Examples

This is an example of a Producer/Consumer pattern.

The Producer creates jobs (here represented by integers in the global table tab).  The Consumers remove a job, process it, then insert the result back into the table.

We have two condition variables and one mutex.  One condition variable (cp) is used for the Producer thread.  The Producer thread will pause on the producer condition variable if enough jobs are available for processing. The other condition variable (cc) is used for the Consumer threads.  When no jobs are available, the Consumer threads will pause on that condition variable until the Producer creates more jobs.

The mutex is used to protect access to the global variable tab.

 > $\mathrm{tab}≔\mathrm{table}\left(\right):$
 > $m≔\mathrm{Threads}[\mathrm{Mutex}][\mathrm{Create}]\left(\right)$
 ${m}{:=}{2}$ (1)
 > $\mathrm{cc}≔\mathrm{Threads}[\mathrm{ConditionVariable}][\mathrm{Create}]\left(\right)$
 ${\mathrm{cc}}{:=}{1}$ (2)
 > $\mathrm{cp}≔\mathrm{Threads}[\mathrm{ConditionVariable}][\mathrm{Create}]\left(\right)$
 ${\mathrm{cp}}{:=}{2}$ (3)
 > $e≔\mathrm{true}:$

Start the Producer thread. We wait on cp until the Producer thread has started.

 > $\mathrm{Threads}[\mathrm{Mutex}][\mathrm{Lock}]\left(m\right)$
 > $\mathrm{id1}≔\mathrm{Threads}[\mathrm{Create}]\left(\mathrm{Producer}\left(m,\mathrm{cp},\mathrm{cc},31,10\right)\right)$
 ${\mathrm{id1}}{:=}{1}$ (4)
 > $\mathrm{Threads}[\mathrm{ConditionVariable}][\mathrm{Wait}]\left(\mathrm{cp},m\right)$
 > $\mathrm{Threads}[\mathrm{Mutex}][\mathrm{Unlock}]\left(m\right)$

Start the Consumer threads.  They will each consume 100 jobs and there are 5 threads so we should process 500 jobs.

 > $\mathrm{id2}≔\left[\mathrm{seq}\left(\mathrm{Threads}[\mathrm{Create}]\left(\mathrm{Consumer}\left(m,\mathrm{cp},\mathrm{cc},100\right)\right),i=1..5\right)\right]$
 ${\mathrm{id2}}{:=}\left[{2}{,}{3}{,}{4}{,}{5}{,}{6}\right]$ (5)

Wait for the Consumer threads to finish.

 > $\mathrm{Threads}[\mathrm{Wait}]\left(\mathrm{op}\left(\mathrm{id2}\right)\right)$
 > $\mathrm{Threads}[\mathrm{Mutex}][\mathrm{Lock}]\left(m\right)$

 > $e≔\mathrm{false}:$
 > $\mathrm{Threads}[\mathrm{ConditionVariable}][\mathrm{Signal}]\left(\mathrm{cp}\right)$
 > $\mathrm{Threads}[\mathrm{Mutex}][\mathrm{Unlock}]\left(m\right)$
 > $\mathrm{Threads}[\mathrm{Wait}]\left(\mathrm{id1}\right)$

Check the number of processed jobs.

 > $\mathrm{print}\left({\mathrm{tab}}_{"curjob"}\right)$
 ${501}$ (6)

Check the results of one job.

 > $\mathrm{print}\left({\mathrm{tab}}_{233}\right)$
 ${108811}$ (7)
 > $\mathrm{print}\left(\mathrm{add}\left(i,i=1..2\cdot 233\right)\right)$
 ${108811}$ (8)