Re: [swift-users] Counting in Threads

2016-10-12 Thread Daniel Dunbar via swift-users
Not in this case, today: -- $ cat x.swift import Darwin.C public class AtomicInt32 { public fileprivate (set) var value : Int32 = 0 /// Create a new atomic integer with the specified initial value. public init(_ value: Int32 = 0) { self.value = value } /// Add one to the

Re: [swift-users] Counting in Threads

2016-10-12 Thread Philippe Hausler via swift-users
Or allocate a pointer for it. The other alternative would be NSLock but that might be swatting flies with sledgehammers. We had a similar issue implementing NSLock in swift-corelibs-foundation where taking the address of a pthread mutex corrupted the structure for when another thread would

Re: [swift-users] Counting in Threads

2016-10-12 Thread Shawn Erickson via swift-users
So we would have to drop down to C code, etc. to safely leverage OSAtomic? On Wed, Oct 12, 2016 at 8:32 AM Philippe Hausler via swift-users < swift-users@swift.org> wrote: > I was under the impression that taking the address was more than a single > load instruction and would emit a placeholder

Re: [swift-users] Counting in Threads

2016-10-12 Thread Daniel Dunbar via swift-users
I suspect one of the actual compiler people might tell me I shouldn't trust this, but in practice it works: -- import Darwin.C public class AtomicInt32 { public fileprivate (set) var value : Int32 = 0 /// Create a new atomic integer with the specified initial value. public

[swift-users] Counting in Threads

2016-10-12 Thread Gerriet M. Denkmann via swift-users
How to translate this to Swift: __block atomic_uint_fast64_t counter = ATOMIC_VAR_INIT(0); dispatch_apply( nbrInterations, queue, ^void(size_t idx) { uint64_t tCount = 0; ... do some counting ... atomic_fetch_add_explicit( , tCount,