<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">I am sorry if this bothers you. I just put the original code in Xcode and it works. Anyone knows why this works in Xcode or in macOS? I thought it should show the same warnings as Linux.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">Zhaoxin</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Mar 3, 2017 at 2:51 AM, Edward Connell via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</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">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="m_-3801591860844577408gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>let count = 1000</div><div><span class="m_-3801591860844577408gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>let items = UnsafeMutablePointer<[UInt8]?><wbr>.allocate(capacity: count)</div><span class=""><div><span class="m_-3801591860844577408gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>items.initialize(to: nil, count: count)</div><div><br></div><div><span class="m_-3801591860844577408gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>DispatchQueue.<wbr>concurrentPerform(iterations: count) {</div></span><span class=""><div><span class="m_-3801591860844577408gmail-Apple-tab-span" style="white-space:pre-wrap">                </span>items[$0] = [UInt8](repeating: 7, count: 10)</div><div><span class="m_-3801591860844577408gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>}</div><div><br></div></span><div><span class="m_-3801591860844577408gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>items.deinitialize(count: count)<br></div><div><span class="m_-3801591860844577408gmail-Apple-tab-span" style="white-space:pre-wrap">        </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><span class=""><div> #0 free <null> (libtsan.so.0+0x000000025819)</div><div> #1 _TwXxOs31_<wbr>ClosedRangeIndexRepresentation <null> (libswiftCore.so+<wbr>0x00000027e071)</div><div><br></div></span><div> Previous write of size 8 at 0x7d0c00005fa0 by thread T20:</div><span class=""><div> #0 malloc <null> (libtsan.so.0+0x0000000254a3)</div><div> #1 swift_slowAlloc <null> (libswiftCore.so+<wbr>0x0000002ee3e5)</div><div><br></div></span><div> Thread T20 (tid=24288, running) created by main thread at:</div><span class=""><div> #0 pthread_create <null> (libtsan.so.0+0x000000027577)</div><div> #1 manager_workqueue_additem /home/buildnode/disk2/<wbr>workspace/oss-swift-3.0-<wbr>package-linux-ubuntu-16_04/<wbr>swift-corelibs-libdispatch/<wbr>libpwq/src/posix/manager.c:815 (libdispatch.so+<wbr>0x00000007c6b1)</div><div><br></div><div>SUMMARY: ThreadSanitizer: data race ??:0 __interceptor_free</div><div>==================</div><div><br></div><div><br></div></span><div>ThreadSanitizer: reported 1 warnings</div></div><div><br></div><div><br></div></div><div class="HOEnZb"><div class="h5"><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="m_-3801591860844577408HOEnZb"><div class="m_-3801591860844577408h5"><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>
</div></div><br>______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>