<div dir="ltr">Hi Guillaume,<div>There is still a race condition report, but I think maybe we are almost there. I had to modify your example because it didn't quite build, and a loop to do it twice since it randomly complains if you only do it once. Btw, this is on Linux.</div><div>The warning is triggered when deinitialize is called. It wasn't in your example, but it seems that it is required since the Elements are not a trivial type like Int.</div><div><br></div><div>Thanks, Ed</div><div><br></div><div><div>for _ in 0..<2 {</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>let count = 1000</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>let items = UnsafeMutablePointer<[UInt8]?>.allocate(capacity: count)</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>items.initialize(to: nil, count: count)</div><div><br></div><div><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>DispatchQueue.concurrentPerform(iterations: count) {</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                </span>items[$0] = [UInt8](repeating: 7, count: 10)</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>}</div><div><br></div><div><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>items.deinitialize(count: count)<br></div><div><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>items.deallocate(capacity: count)</div><div>}</div></div><div><br></div><div><div>==================</div><div>WARNING: ThreadSanitizer: data race (pid=24076)</div><div> Write of size 8 at 0x7d0c00005fa0 by main thread:</div><div> #0 free <null> (libtsan.so.0+0x000000025819)</div><div> #1 _TwXxOs31_ClosedRangeIndexRepresentation <null> (libswiftCore.so+0x00000027e071)</div><div><br></div><div> Previous write of size 8 at 0x7d0c00005fa0 by thread T20:</div><div> #0 malloc <null> (libtsan.so.0+0x0000000254a3)</div><div> #1 swift_slowAlloc <null> (libswiftCore.so+0x0000002ee3e5)</div><div><br></div><div> Thread T20 (tid=24288, running) created by main thread at:</div><div> #0 pthread_create <null> (libtsan.so.0+0x000000027577)</div><div> #1 manager_workqueue_additem /home/buildnode/disk2/workspace/oss-swift-3.0-package-linux-ubuntu-16_04/swift-corelibs-libdispatch/libpwq/src/posix/manager.c:815 (libdispatch.so+0x00000007c6b1)</div><div><br></div><div>SUMMARY: ThreadSanitizer: data race ??:0 __interceptor_free</div><div>==================</div><div><br></div><div><br></div><div>ThreadSanitizer: reported 1 warnings</div></div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Mar 1, 2017 at 6:48 PM, Edward Connell <span dir="ltr"><<a href="mailto:ewconnell@gmail.com" target="_blank">ewconnell@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Ahh! thank you. That makes sense. </div><div class="HOEnZb"><div class="h5"><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><br>
> On Mar 1, 2017, at 3:21 PM, Edward Connell via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">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.concurrentPerfo<wbr>rm(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.concurrentPerfor<wbr>m(iterations: count) {<br>
items[$0].initialize([UInt8](r<wbr>epeating: 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>
</div></div></blockquote></div><br></div>