我想在后台执行一些任务并添加取消任务的功能。
类似于:
while !isCanceled && someOtherCondition() {
doSome()
}
并从另一个线程取消任务:
func cancel() {
isCanceled = true
}
在Java中,我可以将isCancelled声明为 volatile 的,这就足够了。我怎样才能在 Swift 中做到这一点?
最佳答案
看来这就是你的答案:
Swift does not (yet) have a well-defined memory model, so there’s no direct equivalent of C’s volatile. Having said that, C didn’t have a well-defined memory model until C11 either, so you can go a long way without one.
关于multithreading - 如何在 Swift 中创建 volatile bool 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34687797/