<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Not in this case, today:<div class="">--</div><div class=""><div class="">$ cat x.swift </div><div class="">import Darwin.C</div><div class=""><br class=""></div><div class="">public class AtomicInt32 {</div><div class=""> public fileprivate (set) var value : Int32 = 0</div><div class=""><br class=""></div><div class=""> /// Create a new atomic integer with the specified initial value.</div><div class=""> public init(_ value: Int32 = 0) {</div><div class=""> self.value = value</div><div class=""> }</div><div class=""><br class=""></div><div class=""> /// Add one to the value.</div><div class=""> public func increment () {</div><div class=""> OSAtomicIncrement32(&value)</div><div class=""> }</div><div class="">}</div><div class=""><br class=""></div><div class="">$ swiftc -S -o - x.swift -O | grep '.globl<span class="Apple-tab-span" style="white-space: pre;">        </span>__TFC4main11AtomicInt329incrementfT_T_' -A16</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>.globl<span class="Apple-tab-span" style="white-space:pre">        </span>__TFC4main11AtomicInt329incrementfT_T_</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>.align<span class="Apple-tab-span" style="white-space:pre">        </span>4, 0x90</div><div class="">__TFC4main11AtomicInt329incrementfT_T_:</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>.cfi_startproc</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>pushq<span class="Apple-tab-span" style="white-space:pre">        </span>%rbp</div><div class="">Ltmp4:</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>.cfi_def_cfa_offset 16</div><div class="">Ltmp5:</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>.cfi_offset %rbp, -16</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>movq<span class="Apple-tab-span" style="white-space:pre">        </span>%rsp, %rbp</div><div class="">Ltmp6:</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>.cfi_def_cfa_register %rbp</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>leaq<span class="Apple-tab-span" style="white-space:pre">        </span>16(%rdi), %rsi</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>movl<span class="Apple-tab-span" style="white-space:pre">        </span>$1, %edi</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>popq<span class="Apple-tab-span" style="white-space:pre">        </span>%rbp</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>jmp<span class="Apple-tab-span" style="white-space:pre">        </span>_OSAtomicAdd32</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>.cfi_endproc</div><div class="">--</div><div class=""><br class=""></div><div class=""> - Daniel</div><div class=""><br class=""></div><div><blockquote type="cite" class=""><div class="">On Oct 12, 2016, at 8:31 AM, Philippe Hausler <<a href="mailto:phausler@apple.com" class="">phausler@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div dir="auto" class=""><div class=""><div style="direction: inherit;" class="">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="">Sent from my iPhone</div><div class=""><br class="">On Oct 12, 2016, at 8:18 AM, Daniel Dunbar via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><span class="">I suspect one of the actual compiler people might tell me I shouldn't trust this, but in practice it works:</span><br class=""><span class="">--</span><br class=""><span class="">import Darwin.C</span><br class=""><span class=""></span><br class=""><span class="">public class AtomicInt32 {</span><br class=""><span class=""> public fileprivate (set) var value : Int32 = 0</span><br class=""><span class=""></span><br class=""><span class=""> /// Create a new atomic integer with the specified initial value.</span><br class=""><span class=""> public init(_ value: Int32 = 0) {</span><br class=""><span class=""> self.value = value</span><br class=""><span class=""> }</span><br class=""><span class=""></span><br class=""><span class=""> /// Add one to the value.</span><br class=""><span class=""> public func increment () {</span><br class=""><span class=""> OSAtomicIncrement32(&value)</span><br class=""><span class=""> }</span><br class=""><span class="">}</span><br class=""><span class=""></span><br class=""><span class="">public func +=(int: AtomicInt32, value: Int32) {</span><br class=""><span class=""> OSAtomicAdd32(value, &int.value)</span><br class=""><span class="">}</span><br class=""><span class="">--</span><br class=""><span class=""></span><br class=""><span class="">Would also love to know if compiler guarantees I *can* trust this.</span><br class=""><span class=""></span><br class=""><span class="">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=""><span class=""></span><br class=""><span class=""> - Daniel</span><br class=""><span class=""></span><br class=""><blockquote type="cite" class=""><span class="">On Oct 12, 2016, at 12:47 AM, Gerriet M. Denkmann via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</span><br class=""></blockquote><blockquote type="cite" class=""><span class=""></span><br class=""></blockquote><blockquote type="cite" class=""><span class=""></span><br class=""></blockquote><blockquote type="cite" class=""><span class="">How to translate this to Swift:</span><br class=""></blockquote><blockquote type="cite" class=""><span class=""></span><br class=""></blockquote><blockquote type="cite" class=""><span class="">__block atomic_uint_fast64_t counter = ATOMIC_VAR_INIT(0);</span><br class=""></blockquote><blockquote type="cite" class=""><span class="">dispatch_apply( nbrInterations, queue, ^void(size_t idx) </span><br class=""></blockquote><blockquote type="cite" class=""><span class=""> {</span><br class=""></blockquote><blockquote type="cite" class=""><span class=""> uint64_t tCount = 0;</span><br class=""></blockquote><blockquote type="cite" class=""><span class=""> ... do some counting ...</span><br class=""></blockquote><blockquote type="cite" class=""><span class=""> atomic_fetch_add_explicit( &counter, tCount, memory_order_relaxed );</span><br class=""></blockquote><blockquote type="cite" class=""><span class=""> }</span><br class=""></blockquote><blockquote type="cite" class=""><span class="">)</span><br class=""></blockquote><blockquote type="cite" class=""><span class=""></span><br class=""></blockquote><blockquote type="cite" class=""><span class="">Currently I am using:</span><br class=""></blockquote><blockquote type="cite" class=""><span class=""></span><br class=""></blockquote><blockquote type="cite" class=""><span class="">var counter: UInt64 = 0</span><br class=""></blockquote><blockquote type="cite" class=""><span class="">let dsema = DispatchSemaphore(value: 1) </span><br class=""></blockquote><blockquote type="cite" class=""><span class="">DispatchQueue.concurrentPerform( iterations: nbrInterations )</span><br class=""></blockquote><blockquote type="cite" class=""><span class="">{ ( idx: size_t) -> Void in</span><br class=""></blockquote><blockquote type="cite" class=""><span class=""> </span><br class=""></blockquote><blockquote type="cite" class=""><span class=""> var tCount: UInt64 = 0</span><br class=""></blockquote><blockquote type="cite" class=""><span class=""> ... do some counting ...</span><br class=""></blockquote><blockquote type="cite" class=""><span class=""> _ = dsema.wait(timeout: .distantFuture) </span><br class=""></blockquote><blockquote type="cite" class=""><span class=""> counter += tCount;</span><br class=""></blockquote><blockquote type="cite" class=""><span class=""> dsema.signal() </span><br class=""></blockquote><blockquote type="cite" class=""><span class="">}</span><br class=""></blockquote><blockquote type="cite" class=""><span class=""></span><br class=""></blockquote><blockquote type="cite" class=""><span class="">Is there a better way?</span><br class=""></blockquote><blockquote type="cite" class=""><span class=""></span><br class=""></blockquote><blockquote type="cite" class=""><span class="">Gerriet.</span><br class=""></blockquote><blockquote type="cite" class=""><span class=""></span><br class=""></blockquote><blockquote type="cite" class=""><span class="">_______________________________________________</span><br class=""></blockquote><blockquote type="cite" class=""><span class="">swift-users mailing list</span><br class=""></blockquote><blockquote type="cite" class=""><span class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a></span><br class=""></blockquote><blockquote type="cite" class=""><span class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" class="">https://lists.swift.org/mailman/listinfo/swift-users</a></span><br class=""></blockquote><span class=""></span><br class=""><span class="">_______________________________________________</span><br class=""><span class="">swift-users mailing list</span><br class=""><span class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a></span><br class=""><span class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" class="">https://lists.swift.org/mailman/listinfo/swift-users</a></span><br class=""></div></blockquote></div></div></blockquote></div><br class=""></div></body></html>