> On Mar 1, 2017, at 3:21 PM, Edward Connell via swift-users 
> <swift-users@swift.org> wrote:
> 
> The thread sanitizer on Linux is reporting that I have race conditions in 
> libswiftcore. I eliminated enough code down to this trivial example. Is there 
> really a race condition here or are these bogus errors?
> 
>       let count = 1000
>       var items = [[UInt8]?](repeating: nil, count: count)
> 
>       DispatchQueue.concurrentPerform(iterations: count) {
>               items[$0] = [UInt8](repeating: 7, count: 10)
>       }
> 
> My real scenario is retrieving data asynchronously, so I just threw in a 
> buffer assignment.

The assignments to array elements are where the race lies.

I don’t know about the libswiftcore part, but: assigning to a shared Array 
concurrently from multiple threads won't work, because of Array's copy-on-write 
behaviour. You could do

let items = UnsafeMutablePointer<[UInt8]?>.allocate(capacity: 1)
items.initialize(to: nil, count: count)

DispatchQueue.concurrentPerform(iterations: count) {
  items[$0].initialize([UInt8](repeating: 7, count: 10))
}

// you’ll be able to see here that they’re all initialized

items.deallocate(capacity: count)

Cheers,
Guillaume Lessard

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Reply via email to