<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - NONDETERMINIZM: It seems, ThreadSanitizer does not understand OpenMP's critical section in C++"
   href="https://bugs.llvm.org/show_bug.cgi?id=32840">32840</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>NONDETERMINIZM: It seems, ThreadSanitizer does not understand OpenMP's critical section in C++
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>-New Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>lebedev.ri@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Example one: (good)

#include <stdio.h>

int main() {
#ifdef _OPENMP
#pragma omp parallel num_threads(2)
#endif
#ifdef _OPENMP
#pragma omp critical(io)
#endif
  {
    printf("thread\n");
  }

  return 0;
}

$ clang-5.0 openmp-critical.c -fsanitize=thread
$ ./a.out 
thread

As you can see, TSan does not complain, and there were only one print.
Let's enable OpenMP.

$ clang-5.0 openmp-critical.c -fsanitize=thread -fopenmp
$ ./a.out 
thread
thread

As you can see, TSan does not complain, and there were two prints.
Now, let's check C++.

Example two (bad):

#include <cstdio>
#include <iostream>

int main() {
#ifdef _OPENMP
#pragma omp parallel num_threads(2)
#endif
#ifdef _OPENMP
#pragma omp critical(io)
#endif
  {
    std::cout << "thread\n";
  }

  return 0;
}

$ clang++-5.0 openmp-critical.cpp -fsanitize=thread 
$ ./a.out 
thread

Again all fine. Let's enable OpenMP.

$ clang++-5.0 openmp-critical.cpp -fsanitize=thread -fopenmp
$ ./a.out 
==================
WARNING: ThreadSanitizer: data race (pid=30729)
  Atomic read of size 1 at 0x7b6800000b00 by main thread:
    #0 pthread_mutex_lock <null> (a.out+0x432067)
    #1 <null> <null> (libomp.so.5+0x477a2)
    #2 __libc_start_main <null> (libc.so.6+0x202b0)

  Previous write of size 1 at 0x7b6800000b00 by thread T2:
    #0 pthread_mutex_init <null> (a.out+0x42ae0a)
    #1 <null> <null> (libomp.so.5+0x44c51)

  Location is heap block of size 1440 at 0x7b6800000600 allocated by main
thread:
    #0 malloc <null> (a.out+0x4296dd)
    #1 getenv <null> (libomp.so.5+0x18202)
    #2 __libc_start_main <null> (libc.so.6+0x202b0)

  Thread T2 (tid=30732, running) created by main thread at:
    #0 pthread_create <null> (a.out+0x42ab26)
    #1 <null> <null> (libomp.so.5+0x458bb)
    #2 __libc_start_main <null> (libc.so.6+0x202b0)

SUMMARY: ThreadSanitizer: data race (/tmp/a.out+0x432067) in pthread_mutex_lock
==================
thread
thread
ThreadSanitizer: reported 1 warnings

I'd expect that it should not complain. 

And now the weirdest part: it only happens if the system is not in idle, i.e.
if there are some other computational/compilation processes running!</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>