如何停止在调度队列中执行任务?

如何停止在调度队列中执行任务?

问题描述:

如果我有一个串行队列,我怎么能从主线程告诉它立即停止执行并取消它的所有任务?

If I have a serial queue, how can I, from the main thread, tell it to immediately stop execution and cancel all of its tasks?

这是一个非常常见的问题,我之前已经回答过:

This is a pretty common question, and one I've answered before:

暂停GCD查询问题

简短的回答是GCD没有取消API;你必须自己实施你的取消代码。在上面的答案中,我基本上展示了如何做到这一点。

The short answer is that GCD doesn't have a cancellation API; you have to implement your cancellation code yourself. In my answer, above, I show basically how that can be done.