vwait(3tcl)
β¨ vwait β wait for a variable to get written, bestie β¨
okay so you're in the Tcl Built-In Commands corner of the manual and you found vwait. it's giving "block the whole app until something happens" energy and honestly? iconic. let's get into it π
π¦ NAME π¦
vwait β process events until a variable is written
πΈ SYNOPSIS πΈ
vwait varName
vwait ?options? ?varName ...?
π DESCRIPTION π
so basically this command walks into the Tcl event loop and justβ¦ processes events. if nothing's ready it blocks the application. it keeps processing events until some event handler sets the value of the global variable varName. once varName has been set, vwait returns as soon as the event handler that modified varName finishes up. and listen β varName is always interpreted as a variable name with respect to the global namespace, but it can refer to any namespace's variables if you give it the fully-qualified name. periodt.
now, the second, more complex command form has options that let you control the wait operation way more finely and deal with multiple event sources. the options can be made up of:
--β marks the end of options. all the following arguments get handled as variable names.-allβ all the conditions for the wait operation have to be met to complete the wait operation. otherwise (and this is the default, bestie) the first event completes the wait.-extendedβ an extended result in list form gets returned, peep below for the explanation.-nofileeventsβ file events are not handled in the wait operation.-noidleeventsβ idle handlers are not invoked during the wait operation.-notimereventsβ timer handlers are not serviced during the wait operation.-nowindoweventsβ events of the windowing system are not handled during the wait operation.-readablechannel β channel must name a Tcl channel open for reading. if channel is or becomes readable, the wait operation completes.-timeoutmilliseconds β the wait operation is constrained to milliseconds.-variablevarName β varName must be the name of a global variable. writing or unsetting this variable completes the wait operation.-writablechannel β channel must name a Tcl channel open for writing. if channel is or becomes writable, the wait operation completes.
the result returned by vwait is an empty string for the simple form. if the -timeout option is specified, the result is the number of milliseconds remaining when the wait condition has been met, or -1 if the wait operation timed out.
if the -extended option is specified, the result is made up of a Tcl list with an even number of elements. odd elements take the values readable, timeleft, variable, and writable. even elements are the corresponding variable and channel names, or the remaining number of milliseconds. the list is ordered by the occurrences of the event(s) β with the exception of timeleft, which always comes last. iykyk π
in some cases the vwait command may not return immediately after varName et.al. is set. this happens if the event handler that sets varName does not complete immediately. for example, if an event handler sets varName and then itself calls vwait to wait for a different variable, then it may not return for a long time. during this time the top-level vwait is blocked waiting for the event handler to complete, so it cannot return either. (see the NESTED VWAITS BY EXAMPLE below, it's a whole thing π)
to be clear: multiple vwait calls will nest and will not happen in parallel. the outermost call to vwait will not return until all the inner ones do. it is recommended that code should never nest vwait calls (by avoiding putting them in event callbacks), but when that is not possible, care should be taken to add interlock variables to the code to prevent all reentrant calls to vwait that are not strictly necessary. be aware that the synchronous modes of operation of some Tcl packages (e.g., http) use vwait internally; if using the event loop, it is best to use the asynchronous callback-based modes of operation of those packages where available. no cap, that's the safe route π«Ά
π₯ EXAMPLES π₯
run the event-loop continually until some event calls exit. (you can use any variable not mentioned elsewhere, but the name forever reminds you at a glance of the intent.)
vwait forever
wait five seconds for a connection to a server socket, otherwise close the socket and continue running the script:
# Initialise the state
after 5000 set state timeout
set server [socket -server accept 12345]
proc accept {args} {
global state connectionInfo
set state accepted
set connectionInfo $args
}
# Wait for something to happen
vwait state
# Clean up events that could have happened
close $server
after cancel set state timeout
# Do something based on how the vwait finished...
switch $state {
timeout {
puts "no connection on port 12345"
}
accepted {
puts "connection: $connectionInfo"
puts [lindex $connectionInfo 0] "Hello there!"
}
}
a command that will wait for some time delay by waiting for a namespace variable to be set. includes an interlock to prevent nested waits. π§
namespace eval example {
variable v done
proc wait {delay} {
variable v
if {$v ne "waiting"} {
set v waiting
after $delay [namespace code {set v done}]
vwait [namespace which -variable v]
}
return $v
}
}
when running inside a coroutine, an alternative to using vwait is to yield to an outer event loop and to get recommenced when the variable is set, or at an idle moment after that. β¨
coroutine task apply {{} {
# simulate [after 1000]
after 1000 [info coroutine]
yield
# schedule the setting of a global variable, as normal
after 2000 {set var 1}
# simulate [vwait var]
proc updatedVar {task args} {
after idle $task
trace remove variable ::var write "updatedVar $task"
}
trace add variable ::var write "updatedVar [info coroutine]"
yield
}}
π NESTED VWAITS BY EXAMPLE π
this example demonstrates what can happen when the vwait command is nested. the script will never finish because the waiting for the a variable never finishes; that vwait command is still waiting for a script scheduled with after to complete, which just happens to be running an inner vwait (for b) even though the event that the outer vwait was waiting for (the setting of a) has occurred. it's giving deadlock π
after 500 {
puts "waiting for b"
vwait b
puts "b was set"
}
after 1000 {
puts "setting a"
set a 10
}
puts "waiting for a"
vwait a
puts "a was set"
puts "setting b"
set b 42
if you run the above code, you get this output:
waiting for a
waiting for b
setting a
the script will never print βa was setβ until after it has printed βb was setβ because of the nesting of vwait commands, and yet b will not be set until after the outer vwait returns, so the script has deadlocked. the only ways to avoid this are to either structure the overall program in continuation-passing style or to use coroutine to make the continuations implicit. the first of these options would be written as:
after 500 {
puts "waiting for b"
trace add variable b write {apply {args {
global a b
trace remove variable ::b write \
[lrange [info level 0] 0 1]
puts "b was set"
set ::done ok
}}}
}
after 1000 {
puts "setting a"
set a 10
}
puts "waiting for a"
trace add variable a write {apply {args {
global a b
trace remove variable a write [lrange [info level 0] 0 1]
puts "a was set"
puts "setting b"
set b 42
}}}
vwait done
the second option, with coroutine and some helper procedures, is done like this:
# A coroutine-based wait-for-variable command
proc waitvar globalVar {
trace add variable ::$globalVar write \
[list apply {{v c args} {
trace remove variable $v write \
[lrange [info level 0] 0 3]
after 0 $c
}} ::$globalVar [info coroutine]]
yield
}
# A coroutine-based wait-for-some-time command
proc waittime ms {
after $ms [info coroutine]
yield
}
coroutine task-1 eval {
puts "waiting for a"
waitvar a
puts "a was set"
puts "setting b"
set b 42
}
coroutine task-2 eval {
waittime 500
puts "waiting for b"
waitvar b
puts "b was set"
set done ok
}
coroutine task-3 eval {
waittime 1000
puts "setting a"
set a 10
}
vwait done
π SEE ALSO π
β KEYWORDS β
asynchronous I/O, event, variable, wait
and that's the whole page, bestie!! π this is from the Tcl manual, version 8.0, and the page itself is vwait(3tcl). go forth and not deadlock ur event loop πβ¨
π¦ where this page comes from
- debian trixie: tcl9.0-doc 9.0.1+dfsg-2
- ubuntu questing: tcl9.0-doc 9.0.2+dfsg-1
- ubuntu resolute: tcl9.0-doc 9.0.3+dfsg-1
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 1995-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms