thread cancel
Stops the script that runs in a thread.
thread::cancel ?-unwind? ID ?RESULT?
Description
The command thread::cancel stops the script that runs in the thread ID at its next safe point, with the error eval canceled, or with RESULT as the message. A catch in the script can catch that error, and the thread keeps serving messages. A cancel of a thread that runs no script has no effect. The main thread cannot be the target.
A thread that runs in a child interpreter sees the cancel when the call into the child returns. In the examples, a loop waits until the script runs, and the examples run with --allow thread --allow event.
Arguments
| Argument | Type | Default | Description |
|---|---|---|---|
| ID | thread id | required | The thread to stop. |
| RESULT | string | eval canceled | The message of the error. |
Options
| Option | Type | Default | Description |
|---|---|---|---|
-unwind | flag | none | Gives an error that catch cannot stop, and ends the thread. |
-unwind
Gives an error that catch cannot stop, and ends the thread.
The error eval unwound goes through each catch of the script, and also ends the thread::wait of the thread, so the thread ends, also when it runs no script. The error goes to the proc of thread::errorproc or to the output.
% proc ep {id info} {}
> thread::errorproc ep
> set t [thread::create -joinable]
> thread::send -async $t {tsv::set s go3 1; catch {while 1 {}}} r
> while {![tsv::exists s go3]} {}
> thread::cancel -unwind $t
> thread::join $t
1% thread::cancel -unwind
wrong # args: should be "thread::cancel ?-unwind? id ?result?"
-errorcode TCL WRONGARGSReturns
An empty string.
Examples
% set t [thread::create]
> thread::send -async $t {tsv::set s go 1; while 1 {}} r
> while {![tsv::exists s go]} {}
> thread::cancel $t
> vwait r
> set r
eval canceled
% set t [thread::create]
> thread::send -async $t {tsv::set s go2 1; while 1 {}} r
> while {![tsv::exists s go2]} {}
> thread::cancel $t stopped
> vwait r
> set r
stopped
# A thread that runs no script does not change.
% set t [thread::create]
> thread::cancel $t
> thread::send $t {expr 4}
4
Errors
% thread::cancel
wrong # args: should be "thread::cancel ?-unwind? id ?result?"
-errorcode TCL WRONGARGS
% thread::cancel [thread::id]
the main thread cannot be canceled
-errorcode NONE
% thread::cancel tid0x99
thread "tid0x99" does not exist
-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