<div style="white-space:pre-wrap">So we would have to drop down to C code, etc. to safely leverage OSAtomic?</div><br><div class="gmail_quote"><div dir="ltr">On Wed, Oct 12, 2016 at 8:32 AM Philippe Hausler via swift-users &lt;<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto" class="gmail_msg"><div class="gmail_msg"><div style="direction:inherit" class="gmail_msg">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 class="gmail_msg">Sent from my iPhone</div></div><div dir="auto" class="gmail_msg"><div class="gmail_msg"><br class="gmail_msg">On Oct 12, 2016, at 8:18 AM, Daniel Dunbar via swift-users &lt;<a href="mailto:swift-users@swift.org" class="gmail_msg" target="_blank">swift-users@swift.org</a>&gt; wrote:<br class="gmail_msg"><br class="gmail_msg"></div><blockquote type="cite" class="gmail_msg"><div class="gmail_msg"><span class="gmail_msg">I suspect one of the actual compiler people might tell me I shouldn&#39;t trust this, but in practice it works:</span><br class="gmail_msg"><span class="gmail_msg">--</span><br class="gmail_msg"><span class="gmail_msg">import Darwin.C</span><br class="gmail_msg"><span class="gmail_msg"></span><br class="gmail_msg"><span class="gmail_msg">public class AtomicInt32 {</span><br class="gmail_msg"><span class="gmail_msg">    public fileprivate (set) var value : Int32 = 0</span><br class="gmail_msg"><span class="gmail_msg"></span><br class="gmail_msg"><span class="gmail_msg">    /// Create a new atomic integer with the specified initial value.</span><br class="gmail_msg"><span class="gmail_msg">    public init(_ value: Int32 = 0) {</span><br class="gmail_msg"><span class="gmail_msg">        self.value = value</span><br class="gmail_msg"><span class="gmail_msg">    }</span><br class="gmail_msg"><span class="gmail_msg"></span><br class="gmail_msg"><span class="gmail_msg">    /// Add one to the value.</span><br class="gmail_msg"><span class="gmail_msg">    public func increment () {</span><br class="gmail_msg"><span class="gmail_msg">        OSAtomicIncrement32(&amp;value)</span><br class="gmail_msg"><span class="gmail_msg">    }</span><br class="gmail_msg"><span class="gmail_msg">}</span><br class="gmail_msg"><span class="gmail_msg"></span><br class="gmail_msg"><span class="gmail_msg">public func +=(int: AtomicInt32, value: Int32) {</span><br class="gmail_msg"><span class="gmail_msg">    OSAtomicAdd32(value, &amp;int.value)</span><br class="gmail_msg"><span class="gmail_msg">}</span><br class="gmail_msg"><span class="gmail_msg">--</span><br class="gmail_msg"><span class="gmail_msg"></span><br class="gmail_msg"><span class="gmail_msg">Would also love to know if compiler guarantees I *can* trust this.</span><br class="gmail_msg"><span class="gmail_msg"></span><br class="gmail_msg"><span class="gmail_msg">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 class="gmail_msg"><span class="gmail_msg"></span><br class="gmail_msg"><span class="gmail_msg"> - Daniel</span><br class="gmail_msg"><span class="gmail_msg"></span><br class="gmail_msg"><blockquote type="cite" class="gmail_msg"><span class="gmail_msg">On Oct 12, 2016, at 12:47 AM, Gerriet M. Denkmann via swift-users &lt;<a href="mailto:swift-users@swift.org" class="gmail_msg" target="_blank">swift-users@swift.org</a>&gt; wrote:</span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg"></span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg"></span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg">How to translate this to Swift:</span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg"></span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg">__block atomic_uint_fast64_t counter = ATOMIC_VAR_INIT(0);</span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg">dispatch_apply( nbrInterations, queue, ^void(size_t idx)    </span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg">    {</span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg">        uint64_t tCount = 0;</span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg">        ... do some counting ...</span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg">        atomic_fetch_add_explicit( &amp;counter, tCount, memory_order_relaxed );</span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg">    }</span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg">)</span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg"></span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg">Currently I am using:</span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg"></span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg">var counter: UInt64 = 0</span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg">let dsema = DispatchSemaphore(value: 1)  </span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg">DispatchQueue.concurrentPerform( iterations: nbrInterations )</span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg">{ ( idx: size_t) -&gt; Void in</span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg">            </span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg">    var tCount: UInt64 = 0</span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg">    ... do some counting ...</span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg">    _ = dsema.wait(timeout: .distantFuture) </span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg">    counter += tCount;</span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg">    dsema.signal()  </span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg">}</span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg"></span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg">Is there a better way?</span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg"></span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg">Gerriet.</span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg"></span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg">_______________________________________________</span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg">swift-users mailing list</span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg"><a href="mailto:swift-users@swift.org" class="gmail_msg" target="_blank">swift-users@swift.org</a></span><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><span class="gmail_msg"><a href="https://lists.swift.org/mailman/listinfo/swift-users" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a></span><br class="gmail_msg"></blockquote><span class="gmail_msg"></span><br class="gmail_msg"><span class="gmail_msg">_______________________________________________</span><br class="gmail_msg"><span class="gmail_msg">swift-users mailing list</span><br class="gmail_msg"><span class="gmail_msg"><a href="mailto:swift-users@swift.org" class="gmail_msg" target="_blank">swift-users@swift.org</a></span><br class="gmail_msg"><span class="gmail_msg"><a href="https://lists.swift.org/mailman/listinfo/swift-users" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a></span><br class="gmail_msg"></div></blockquote></div>_______________________________________________<br class="gmail_msg">
swift-users mailing list<br class="gmail_msg">
<a href="mailto:swift-users@swift.org" class="gmail_msg" target="_blank">swift-users@swift.org</a><br class="gmail_msg">
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br class="gmail_msg">
</blockquote></div>