<div dir="ltr">Not at all, thank you for trying it out.<div>When I run my code on the Mac in Xcode with thread sanitizer on, I don't get any warnings or problems at all. I only find problems on Linux. I have noticed that for the same release, the compiler and libraries are not identical between Mac and Linux. I am currently using what I believe is the latest release for both</div><div><br></div><div><div>$ swift --version</div><div>Swift version 3.0.2 (swift-3.0.2-RELEASE)</div><div>Target: x86_64-unknown-linux-gnu</div></div><div><br></div><div> </div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Mar 2, 2017 at 3:32 PM, Zhao Xin <span dir="ltr"><<a href="mailto:owenzx@gmail.com" target="_blank">owenzx@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"><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"><div><div class="h5">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></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><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_-7930886511067522252m_-3801591860844577408gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>let count = 1000</div><div><span class="m_-7930886511067522252m_-3801591860844577408gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>let items = UnsafeMutablePointer<[UInt8]?><wbr>.allocate(capacity: count)</div><span><div><span class="m_-7930886511067522252m_-3801591860844577408gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>items.initialize(to: nil, count: count)</div><div><br></div><div><span class="m_-7930886511067522252m_-3801591860844577408gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>DispatchQueue.concurrentPerfor<wbr>m(iterations: count) {</div></span><span><div><span class="m_-7930886511067522252m_-3801591860844577408gmail-Apple-tab-span" style="white-space:pre-wrap">                </span>items[$0] = [UInt8](repeating: 7, count: 10)</div><div><span class="m_-7930886511067522252m_-3801591860844577408gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>}</div><div><br></div></span><div><span class="m_-7930886511067522252m_-3801591860844577408gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>items.deinitialize(count: count)<br></div><div><span class="m_-7930886511067522252m_-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><div> #0 free <null> (libtsan.so.0+0x000000025819)</div><div> #1 _TwXxOs31_ClosedRangeIndexRepr<wbr>esentation <null> (libswiftCore.so+0x00000027e07<wbr>1)</div><div><br></div></span><div> Previous write of size 8 at 0x7d0c00005fa0 by thread T20:</div><span><div> #0 malloc <null> (libtsan.so.0+0x0000000254a3)</div><div> #1 swift_slowAlloc <null> (libswiftCore.so+0x0000002ee3e<wbr>5)</div><div><br></div></span><div> Thread T20 (tid=24288, running) created by main thread at:</div><span><div> #0 pthread_create <null> (libtsan.so.0+0x000000027577)</div><div> #1 manager_workqueue_additem /home/buildnode/disk2/workspac<wbr>e/oss-swift-3.0-package-linux-<wbr>ubuntu-16_04/swift-corelibs-<wbr>libdispatch/libpwq/src/posix/<wbr>manager.c:815 (libdispatch.so+0x00000007c6b1<wbr>)</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="m_-7930886511067522252HOEnZb"><div class="m_-7930886511067522252h5"><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_-7930886511067522252m_-3801591860844577408HOEnZb"><div class="m_-7930886511067522252m_-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></div></div><span class="">______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailma<wbr>n/listinfo/swift-users</a><br>
<br></span></blockquote></div><br></div>
</blockquote></div><br></div>