thread send
Runs a script in another thread and gives its result.
thread::send ?-async? ?-head? ID SCRIPT ?VARNAME?
Description
The command thread::send puts SCRIPT in the queue of the thread ID, and the thread runs it at the global level. With no -async, the sender waits for the result and gives it, or the error of the script with its -errorcode. While it waits, the sender runs the scripts that other threads send to it, so two threads that send to each other do not deadlock. A send to the calling thread runs the script at once.
With VARNAME, the result goes to the variable, and the command gives the return code of the script: 0 for a result, and 1 for an error with the message in the variable.
A send that waits gives target thread died with the code THREAD DIED when its target ends first. The examples of this page run with --allow thread --allow event.
Arguments
| Argument | Type | Default | Description |
|---|---|---|---|
| ID | thread id | required | The thread that runs the script. |
| SCRIPT | script | required | The script to run in the thread. |
| VARNAME | variable name | none | The variable of the result. |
Options
| Option | Type | Default | Description |
|---|---|---|---|
-async | flag | none | Queues the script and does not wait for its result. |
-head | flag | none | Puts the script at the head of the queue of the target. |
-async
Queues the script and does not wait for its result.
The command gives an empty string at once. With VARNAME, the variable gets the result later, in the event loop of the sender, so a vwait on it waits for the result. With no VARNAME, an error of the script goes to the proc of thread::errorproc or to the output. With -eventmark on the target, the send waits while the queue of the target is full.
% set t [thread::create]
> thread::send -async $t {set ::x 5}
> thread::send $t {set ::x}
5
% set t [thread::create]
> thread::send -async $t {expr {3 * 3}} v
> vwait v
> set v
9% thread::send -async
wrong # args: should be "thread::send ?-async? ?-head? id script ?varName?"
-errorcode TCL WRONGARGS-head
Puts the script at the head of the queue of the target.
The script goes before the scripts that wait in the queue of the target. A script that already runs ends first.
% set t [thread::create]
> thread::send -head $t {expr 1}
1A flag has no value to check.
Returns
The result of the script, the return code with VARNAME, or an empty string with -async.
Examples
% set t [thread::create]
> thread::send $t {expr {6 * 7}}
42
% set t [thread::create]
> list [thread::send $t {error boom} r] $r
1 boom
% set t [thread::create]
> catch {thread::send $t {error boom {} {MY CODE}}} m o
> dict get $o -errorcode
MY CODE
# A send to the calling thread runs at once.
% thread::send [thread::id] {expr {2 + 2}}
4
Errors
% thread::send
wrong # args: should be "thread::send ?-async? ?-head? id script ?varName?"
-errorcode TCL WRONGARGS
% thread::send tid0x99 {expr 1}
thread "tid0x99" does not exist
-errorcode NONE
% set t [thread::create]
> thread::send $t {error boom}
boom
-errorcode NONE
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