<div dir="ltr">Ahh! thank you. That makes sense. </div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Mar 1, 2017 at 3:29 PM, Guillaume Lessard <span dir="ltr"><<a href="mailto:glessard@tffenterprises.com" target="_blank">glessard@tffenterprises.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
> On Mar 1, 2017, at 3:21 PM, Edward Connell via swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:<br>
><br>
> 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?<br>
><br>
> let count = 1000<br>
> var items = [[UInt8]?](repeating: nil, count: count)<br>
><br>
> DispatchQueue.<wbr>concurrentPerform(iterations: count) {<br>
> items[$0] = [UInt8](repeating: 7, count: 10)<br>
> }<br>
><br>
> My real scenario is retrieving data asynchronously, so I just threw in a buffer assignment.<br>
<br>
</span>The assignments to array elements are where the race lies.<br>
<br>
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<br>
<br>
let items = UnsafeMutablePointer<[UInt8]?><wbr>.allocate(capacity: 1)<br>
items.initialize(to: nil, count: count)<br>
<br>
DispatchQueue.<wbr>concurrentPerform(iterations: count) {<br>
items[$0].initialize([UInt8](<wbr>repeating: 7, count: 10))<br>
}<br>
<br>
// you’ll be able to see here that they’re all initialized<br>
<br>
items.deallocate(capacity: count)<br>
<br>
Cheers,<br>
Guillaume Lessard<br>
<br>
</blockquote></div><br></div>