Thread Cancellation
Cangjie uses a cooperative cancellation mechanism. The cancel() method of Future<T> sends a cancellation request to the target thread but does not forcibly stop thread execution. The thread checks whether there is a cancellation request through the Thread.currentThread.hasPendingCancellation property and decides how to respond.
This design allows developers to control the timing of cancellation and cleanup logic: a thread can exit after completing critical operations, or ignore the cancellation request and continue executing. If a thread doesn't check the cancellation flag, it will continue running until normal completion.
In the example on the right, the main thread first sends a cancellation request, then releases the SyncCounter gate. The worker thread checks the cancellation flag after the gate opens and returns early if it finds it has been cancelled.
SyncCounter is a synchronization primitive. waitUntilZero() blocks until the counter reaches zero, and dec() decrements the counter by 1.