Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Perl

Idiom #229 Cancel an operation

Interrupt an ongoing processing p.

use Coro;
my $coro = async {
   # do work
};
 
$coro->cancel;  # or $coro->safe_cancel

CPAN module Coro provides support for cooperative threads. A thread can cancel another thread using the cancel or safe_cancel methods.
import "context"
ctx, cancel := context.WithCancel(context.Background())
go p(ctx)

somethingElse()

cancel()

Pass a Context to p and execute p.
p is responsible for shutting down gracefully when ctx is canceled

New implementation...
< >
programming-idioms.org