Re: Swift and Threads

2016-09-13 Thread Gerriet M. Denkmann
> On 13 Sep 2016, at 15:20, Quincey Morris > wrote: > >> On Sep 13, 2016, at 00:57 , Gerriet M. Denkmann wrote: >> >> I was struggling to find a solution which is thread safe. > > Your problem didn’t really need thread safety,

Re: Swift and Threads

2016-09-13 Thread Gerriet M. Denkmann
> On 13 Sep 2016, at 14:14, Stephen J. Butler wrote: > > This site suggests a version using withUnsafeMutableBufferPointer: > > http://blog.human-friendly.com/swift-arrays-are-not-threadsafe > > let nbrOfThreads = 8 > let step = 2 > let itemsPerThread = number * step

Re: Swift and Threads

2016-09-13 Thread Quincey Morris
On Sep 12, 2016, at 23:52 , Gerriet M. Denkmann wrote: > > I tried a variation of my function. This does not crash (not even in Debug > builds), but takes twice the memory and is about four times slower: I no longer understand what we’re doing here. Based on the code

Re: Swift and Threads

2016-09-13 Thread Alastair Houghton
On 13 Sep 2016, at 05:29, Jens Alfke wrote: > > The Bool type is one byte in size. > > C++ has a specialization for std::vector that makes it a true bit > array, but I’m not sure if Swift’s generic system is powerful enough to be > able to entirely switch out the

Re: Swift and Threads

2016-09-13 Thread Stephen J. Butler
This site suggests a version using withUnsafeMutableBufferPointer: http://blog.human-friendly.com/swift-arrays-are-not-threadsafe let nbrOfThreads = 8 let step = 2 let itemsPerThread = number * step let bitLimit = nbrOfThreads * itemsPerThread var bitfield = [Bool](count: bitLimit,

Re: Swift and Threads

2016-09-13 Thread Gerriet M. Denkmann
> On 12 Sep 2016, at 22:49, Jens Alfke wrote: > > >> On Sep 12, 2016, at 6:42 AM, Gerriet M. Denkmann >> wrote: >> >> So: is the code ok and the compiler broken in Debug mode? >> Or is the code fundamentally wrong and that it works in Release is

Re: Swift and Threads

2016-09-12 Thread Jens Alfke
> On Sep 12, 2016, at 9:21 PM, Gerriet M. Denkmann wrote: > > READ of size 1776 at 0x61ce68a0 thread T0 >#0 0x1000516f3 in wrap_memcpy (libclang_rt.asan_osx_dynamic.dylib+0x406f3) >#1 0x100f85a88 in __swift_memcpy_array1_1 (libswiftCore.dylib+0x1dea88) >#2

Re: Swift and Threads

2016-09-12 Thread Gerriet M. Denkmann
> On 12 Sep 2016, at 22:49, Jens Alfke wrote: > > >> On Sep 12, 2016, at 6:42 AM, Gerriet M. Denkmann >> wrote: >> >> So: is the code ok and the compiler broken in Debug mode? >> Or is the code fundamentally wrong and that it works in Release is

Re: Swift and Threads

2016-09-12 Thread Jens Alfke
> On Sep 12, 2016, at 11:11 AM, Quincey Morris > wrote: > > My understanding is that pure Swift arrays (that is, *not* bridged NSArrays) > are stored as efficiently as C arrays. It’s not the efficiency of storage I was concerned about; in this case it’s

Re: Swift and Threads

2016-09-12 Thread Quincey Morris
On Sep 12, 2016, at 10:16 , Jens Alfke wrote: > > The question in my mind is whether the array struct ever gets copied in this > code — it’s not explicitly passed as a parameter, just closed over. I don’t > think I’ve read any docs that describe explicitly what the behavior

Re: Swift and Threads

2016-09-12 Thread Jens Alfke
> On Sep 12, 2016, at 9:34 AM, Jean-Daniel wrote: > > Array are value based in Swift and so use COW optimization, which mean that > even if you don’t resize them, you may hit a race condition issue. Yes, you’re right. The question in my mind is whether the array struct ever

Re: Swift and Threads

2016-09-12 Thread Jens Alfke
> On Sep 12, 2016, at 6:42 AM, Gerriet M. Denkmann wrote: > > So: is the code ok and the compiler broken in Debug mode? > Or is the code fundamentally wrong and that it works in Release is just a > fluke? 99% of the time it’s the latter. Which means you should try to

Swift and Threads

2016-09-12 Thread Gerriet M. Denkmann
This function works flawlessly in Release build: func markAndTell( talk: Bool, number: Int) { let nbrOfThreads = 8 let step = 2 let itemsPerThread = number * step let bitLimit = nbrOfThreads * itemsPerThread var bitfield = [Bool](count: bitLimit,