beetlebug▾
/

thread cond

Makes and uses the condition variables of the group.

thread::cond create
thread::cond wait COND MUTEX ?MS?
thread::cond notify COND
thread::cond destroy COND

Description

The command thread::cond works on condition variables. create makes one and gives its handle, such as cid0. wait unlocks MUTEX, waits for a notify of COND or for MS milliseconds, and locks MUTEX again before it returns. The calling thread must hold MUTEX, an exclusive mutex of thread::mutex. notify wakes each thread that waits on COND. destroy removes it, and a destroy of a condition that a thread waits on is an error.

A wait can end with no notify, so test the state that the wait is for in a loop. A wait waits in slices of 50 ms and checks the stop flag of the thread after each one.

Arguments

ArgumentTypeDefaultDescription
CONDcondition handlerequiredA handle of thread::cond create.
MUTEXmutex handlerequiredAn exclusive mutex that the calling thread holds.
MSinteger, millisecondsnoneThe longest wait, or none for a wait that only a notify ends.

Returns

The handle for create, else an empty string.

Examples

% thread::cond create
cid0

# No thread notifies, so the wait ends after 10 ms.
% set c [thread::cond create]
> set m [thread::mutex create]
> thread::mutex lock $m
> thread::cond wait $c $m 10
> thread::mutex unlock $m

% set c [thread::cond create]
> thread::cond notify $c
> thread::cond destroy $c

Errors

% thread::cond
wrong # args: should be "thread::cond option ?args?"
-errorcode TCL WRONGARGS

% thread::cond bogus x
bad option "bogus": must be create, destroy, notify, or wait
-errorcode NONE

% thread::cond wait cid99 mid99
no such condition variable "cid99"
-errorcode NONE

% set c [thread::cond create]
> set m [thread::mutex create]
> thread::cond wait $c $m 10
mutex not locked or wrong type
-errorcode NONE

% thread::cond wait x
wrong # args: should be "thread::cond wait condHandle mutexHandle ?timeout?"
-errorcode TCL WRONGARGS

Facts

Capability
thread. A new interpreter does not have it until the host turns it on.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.1

See also