[swift-users] Atomics and Memory Fences in Swift

Anders Ha hello at andersio.co
Wed Nov 30 07:40:38 CST 2016

Hi guys

I have recently started adopting lock-free atomics with memory fences, but it seems Swift at this moment does not have any native instruments.

Then I read a thread in the Apple Developer Forum (https://forums.developer.apple.com/thread/49334 <https://forums.developer.apple.com/thread/49334>), which an Apple staff claimed that all imported atomic operations are "not guaranteed to be atomic". But for my tests with all optimizations enabled (-Owholemodule and -O), the OSAtomic primitives and stdatomic fences do not seem going wild.

Is these `atomic_*` and `OSAtomic*` primitives really unsafe in Swift as claimed? It doesn't seem like the Swift compiler would reorder memory accesses around a C function call that it wouldn't be able to see through.

P.S. Is any of these primitives available on Linux? It seems the glibc modulemap does not export an `stdatomic` submodule at all.

Best Regards,

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161130/bf6efd7e/attachment.html>

More information about the swift-users mailing list