<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Sat, Oct 28, 2017 at 3:32 AM Rui Ueyama <<a href="mailto:ruiu@google.com">ruiu@google.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Does that mean that std::thread touches a thread object in an unexpected way before it's ready, and all pthread_* functions would work because they are well-behaved?</div></blockquote><div><br></div><div>No. std::thread is not culprit (but it doesn't have PTHREAD_CREATE_DETACHED)</div><div>I can reproduce the crash without std::thread, but with naked pthreads.</div><div>Without "for (...) *p;", pthread_detach() crashes rarely.</div><div>With *p, *p raises segv in the loop (not in the 1st iteration).</div><div> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div></div><div>If so, does calling a pthread function (e.g. pthread_cond_timedwait with timeout value of 1) make it wait until it's ready?</div></div></blockquote><div><br></div><div>I think it doesn't make sense. Rather than waiting for particular condition, I suggest to make the thread detached not in caller context.</div><div><br></div><div>FYI, usleep(1000);pthread_detach() can hide the issue. </div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="gmail_extra"><div class="gmail_quote"></div></div><div class="gmail_extra"><div class="gmail_quote">On Fri, Oct 27, 2017 at 5:56 AM, NAKAMURA Takumi via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">It was discussed in <a href="https://reviews.llvm.org/D39038" target="_blank">https://reviews.llvm.org/D39038</a><div><br></div><div>I suffered when check-lld behaves flaky. For example; <a href="http://lab.llvm.org:8011/builders/llvm-clang-lld-x86_64-scei-ps4-ubuntu-fast/builds/19673" target="_blank">http://lab.llvm.org:8011/builders/llvm-clang-lld-x86_64-scei-ps4-ubuntu-fast/builds/19673</a></div><div><br></div><div>I have been investigating and I guess it triggered bug or something in libpthread or Linux kernel.</div><div>pthread_detach() in std::thread::detach() is crashing with segv.</div><div>Seems pthread (internal struct in Linux nptl) is inaccessible.</div><div>The crash is reproducible w/o std::thread, but with libpthread.</div><div>In my environment, crash can be seen several times per million attempt.</div><div><br></div><div>This is my testcase. (in Parallel.cpp)</div><div><br></div><div><div>        pthread_t th;</div><div>        auto r = pthread_create(&th, nullptr, worker_stub, (void*)this);</div><div>        (void)r;</div><div>        assert(r == 0);</div><div>        auto p = reinterpret_cast<volatile int*>(th);<br></div><div>        for (int j = 0; j < 1000 * 1000; ++j)</div><div>          *p;</div><div>        pthread_detach(th);<br></div></div><div><br></div><div>Accessing *p sometimes raises segv.</div><div><br></div><div>Ideally, Linux (or distro) should be fixed, but I suppose the distro, Ubuntu 14.04 is widely used.</div><div>I would like to propose we could avoid std::thread().detach()</div><div><br></div><div>As far as I tried, this successfully runs to me with million of iterations.</div><div>- Use naked pthread_create()</div><div>  a) Create the thread with PTHREAD_CREATE_DETACHED</div><div>  b) Do pthread_detach(pthread_self()) in the worker thread.</div><div><br></div><div>Note, (b) doesn't work with std::thread.</div><span class="m_-4793577014609495455HOEnZb"><font color="#888888"><div><br></div><div>Takumi</div></font></span></div>
<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
<br></blockquote></div><br></div>
</blockquote></div></div>