<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><div style="direction: inherit;">I was under the impression that taking the address was more than a single load instruction and would emit a placeholder invalid value: which would make that technically unsafe in a threaded context.</div><br>Sent from my iPhone</div><div><br>On Oct 12, 2016, at 8:18 AM, Daniel Dunbar via swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div><span>I suspect one of the actual compiler people might tell me I shouldn't trust this, but in practice it works:</span><br><span>--</span><br><span>import Darwin.C</span><br><span></span><br><span>public class AtomicInt32 {</span><br><span> public fileprivate (set) var value : Int32 = 0</span><br><span></span><br><span> /// Create a new atomic integer with the specified initial value.</span><br><span> public init(_ value: Int32 = 0) {</span><br><span> self.value = value</span><br><span> }</span><br><span></span><br><span> /// Add one to the value.</span><br><span> public func increment () {</span><br><span> OSAtomicIncrement32(&value)</span><br><span> }</span><br><span>}</span><br><span></span><br><span>public func +=(int: AtomicInt32, value: Int32) {</span><br><span> OSAtomicAdd32(value, &int.value)</span><br><span>}</span><br><span>--</span><br><span></span><br><span>Would also love to know if compiler guarantees I *can* trust this.</span><br><span></span><br><span>Note that this has to be a class for this to be in any way safe, which means it is also rather inefficient if the use case was having a lot of them.</span><br><span></span><br><span> - Daniel</span><br><span></span><br><blockquote type="cite"><span>On Oct 12, 2016, at 12:47 AM, Gerriet M. Denkmann via swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>How to translate this to Swift:</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>__block atomic_uint_fast64_t counter = ATOMIC_VAR_INIT(0);</span><br></blockquote><blockquote type="cite"><span>dispatch_apply( nbrInterations, queue, ^void(size_t idx) </span><br></blockquote><blockquote type="cite"><span> {</span><br></blockquote><blockquote type="cite"><span> uint64_t tCount = 0;</span><br></blockquote><blockquote type="cite"><span> ... do some counting ...</span><br></blockquote><blockquote type="cite"><span> atomic_fetch_add_explicit( &counter, tCount, memory_order_relaxed );</span><br></blockquote><blockquote type="cite"><span> }</span><br></blockquote><blockquote type="cite"><span>)</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Currently I am using:</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>var counter: UInt64 = 0</span><br></blockquote><blockquote type="cite"><span>let dsema = DispatchSemaphore(value: 1) </span><br></blockquote><blockquote type="cite"><span>DispatchQueue.concurrentPerform( iterations: nbrInterations )</span><br></blockquote><blockquote type="cite"><span>{ ( idx: size_t) -> Void in</span><br></blockquote><blockquote type="cite"><span> </span><br></blockquote><blockquote type="cite"><span> var tCount: UInt64 = 0</span><br></blockquote><blockquote type="cite"><span> ... do some counting ...</span><br></blockquote><blockquote type="cite"><span> _ = dsema.wait(timeout: .distantFuture) </span><br></blockquote><blockquote type="cite"><span> counter += tCount;</span><br></blockquote><blockquote type="cite"><span> dsema.signal() </span><br></blockquote><blockquote type="cite"><span>}</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Is there a better way?</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Gerriet.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>_______________________________________________</span><br></blockquote><blockquote type="cite"><span>swift-users mailing list</span><br></blockquote><blockquote type="cite"><span><a href="mailto:swift-users@swift.org">swift-users@swift.org</a></span><br></blockquote><blockquote type="cite"><span><a href="https://lists.swift.org/mailman/listinfo/swift-users">https://lists.swift.org/mailman/listinfo/swift-users</a></span><br></blockquote><span></span><br><span>_______________________________________________</span><br><span>swift-users mailing list</span><br><span><a href="mailto:swift-users@swift.org">swift-users@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-users">https://lists.swift.org/mailman/listinfo/swift-users</a></span><br></div></blockquote></body></html>