【发布时间】:2017-11-24 21:31:51
【问题描述】:
有人知道如何在 iOS 10 中制作原子布尔值吗?
当前代码:
import UIKit
struct AtomicBoolean {
fileprivate var val: UInt8 = 0
/// Sets the value, and returns the previous value.
/// The test/set is an atomic operation.
mutating func testAndSet(_ value: Bool) -> Bool {
if value {
return OSAtomicTestAndSet(0, &val)
} else {
return OSAtomicTestAndClear(0, &val)
}
}
/// Returns the current value of the boolean.
/// The value may change before this method returns.
func test() -> Bool {
return val != 0
}
}
代码按预期工作,但我不断收到警告:
'OSAtomicTestAndSet' was deprecated in iOS 10.0: Use atomic_fetch_or_explicit(memory_order_relaxed) from <stdatomic.h> instead
我无法让它与 atomic_fetch_or_explicit(memory_order_relaxed) 一起使用。
有谁知道如何将我当前的代码转换为 iOS 10,以消除此警告?
谢谢!
【问题讨论】:
-
问题的重点大概应该是如何让
atomic_fetch_or_explicit工作。不过,要做到这一点,您需要展示您使用它的尝试,并说明失败的原因。 -
比较stackoverflow.com/questions/39356873/…。 – 编译器警告不推荐使用的 OSAtomic 函数,但不会从
<stdatomic.h>导入函数。
标签: swift swift3 boolean ios10 atomicboolean