<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jun 13, 2014 at 5:29 PM, Zachary Turner <span dir="ltr"><<a href="mailto:zturner@google.com" target="_blank">zturner@google.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">Currently recursive_mutex::~recursive_mutex() asserts if pthread_mutex_destroy fails, and one of the reasons this can fail is if the mutex is locked by another thread.  This leads to the assert being triggered in the following sample program:<div>

<br></div><div><div>#include <mutex></div><div>#include <stdlib.h></div><div>#include <stdio.h></div><div><br></div><div>std::recursive_mutex& getMutex() {</div><div>  static std::recursive_mutex mutex;</div>

<div>  return mutex;</div><div>}</div><div><br></div><div>void* ThreadFunc(void* arg) {</div><div>  std::lock_guard<std::recursive_mutex> Lock(getMutex());</div><div>  exit(1);</div><div>  return nullptr;</div><div>

}</div><div><br></div><div><br></div><div>int main(int argc, char** argv) {</div><div>  pthread_t thread;</div><div>  pthread_attr_t attr;</div><div><br></div><div>  pthread_attr_init(&attr);</div><div>  pthread_create(&thread, &attr, ThreadFunc, nullptr);</div>

</div><div>  pthread_join(thread, nullptr);</div><div><br></div><div>  return 0;</div><div>}</div><div><br></div><div><br></div><div>Is this assertion bogus?  I'm trying to replace a mutex in LLVM with recursive_mutex but am blocked by this because the builder on FreeBSD actually aborts here due to the assertion.  </div>

<div><br></div><div>What's the best way to go about resolving this?  Even if we decide that the assertion should be removed, it's not going to stop the builders from failing every time until they get a new build of libc++, and I don't think (but maybe I'm wrong?) the builders build libc++, but rather just use the one that's already installed.</div>
</div></blockquote></div><br>The C++ spec says that it is UB to destroy a mutex which is locked. I find this completely ridiculous bordering on insanity, but there it is. libc++ is correct here (unless I can get this restriction removed).</div>
<div class="gmail_extra"><br></div><div class="gmail_extra">It's very easy to support this -- just don't call pthread_mutex_destroy(), the function has no purpose really.</div></div>