<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 &lt;<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt; 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> &nbsp;&nbsp;&nbsp;public fileprivate (set) var value : Int32 = 0</span><br><span></span><br><span> &nbsp;&nbsp;&nbsp;/// Create a new atomic integer with the specified initial value.</span><br><span> &nbsp;&nbsp;&nbsp;public init(_ value: Int32 = 0) {</span><br><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.value = value</span><br><span> &nbsp;&nbsp;&nbsp;}</span><br><span></span><br><span> &nbsp;&nbsp;&nbsp;/// Add one to the value.</span><br><span> &nbsp;&nbsp;&nbsp;public func increment () {</span><br><span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OSAtomicIncrement32(&amp;value)</span><br><span> &nbsp;&nbsp;&nbsp;}</span><br><span>}</span><br><span></span><br><span>public func +=(int: AtomicInt32, value: Int32) {</span><br><span> &nbsp;&nbsp;&nbsp;OSAtomicAdd32(value, &amp;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 &lt;<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt; 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) &nbsp; &nbsp;</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp;{</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp; &nbsp; &nbsp;uint64_t tCount = 0;</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp; &nbsp; &nbsp;... do some counting ...</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp; &nbsp; &nbsp;atomic_fetch_add_explicit( &amp;counter, tCount, memory_order_relaxed );</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp;}</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) &nbsp;</span><br></blockquote><blockquote type="cite"><span>DispatchQueue.concurrentPerform( iterations: nbrInterations )</span><br></blockquote><blockquote type="cite"><span>{ ( idx: size_t) -&gt; Void in</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp;var tCount: UInt64 = 0</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp;... do some counting ...</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp;_ = dsema.wait(timeout: .distantFuture) </span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp;counter += tCount;</span><br></blockquote><blockquote type="cite"><span> &nbsp; &nbsp;dsema.signal() &nbsp;</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>