How cancellation works in GCD's DispatchWorkItem: cooperative, not preemptive
In Apple's Grand Central Dispatch, a DispatchWorkItem can be cancelled via cancel(), but the mechanism is cooperative rather than preemptive. If cancel() is called before the work item begins executing, GCD checks the cancellation flag just prior to running the block and skips it entirely. If cancel() is called after the block has already started, it only sets isCancelled to true without interrupting the running code. The block itself must periodically check isCancelled and choose to exit early; otherwise it will run to completion regardless. Additionally, cancel() has no effect on the behavior of wait() or notify(), and a cancelled-but-running item will still block wait() until the block finishes on its own.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in