Skip to content

Modern Chan Cancel

Paul Squires edited this page Oct 19, 2026 · 5 revisions

Home › Goroutines and Channels › Goroutines and Channels › Cancel

Cancel

Cancels the context and every context made from it; a second call is harmless.

Syntax

Cancel( )

Notes

  • Nothing is killed. Cancellation is a request that a goroutine notices, or does not -- TerminateThread would skip destructors and defer blocks and leak every mutex the thread held.
  • Cancelling a parent cancels its children, so one call shuts down a whole tree of workers.
  • Cancelling twice is harmless; the first one wins and Err stays cancelled.
  • After it, IsCancelled is TRUE and the channel from Done is closed -- which is how a select learns about it.

Examples

#import "std/tasks"
using tasks

dim root as Context
dim ctx as CancelContext = WithCancel( root )

print ctx.IsCancelled( )     '' false
ctx.Cancel( )
print ctx.IsCancelled( )     '' true
print ctx.Err( )             '' cancelled

'' Harmless twice -- the first one wins
ctx.Cancel( )
print ctx.Err( )             '' cancelled

'' NOTHING IS KILLED: a goroutine that never checks its context keeps
'' running, and that is deliberate

Library: Goroutines and Channels
Category: Goroutines and Channels
Documented under: Members

Clone this wiki locally