fd_close_norace.cc revision 2d1fdb26e458c4ddc04155c1d421bced3ba90cd0
1// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2#include <pthread.h>
3#include <stdio.h>
4#include <unistd.h>
5#include <sys/types.h>
6#include <sys/stat.h>
7#include <fcntl.h>
8
9void *Thread1(void *x) {
10  int f = open("/dev/random", O_RDONLY);
11  close(f);
12  return NULL;
13}
14
15void *Thread2(void *x) {
16  sleep(1);
17  int f = open("/dev/random", O_RDONLY);
18  close(f);
19  return NULL;
20}
21
22int main() {
23  pthread_t t[2];
24  pthread_create(&t[0], NULL, Thread1, NULL);
25  pthread_create(&t[1], NULL, Thread2, NULL);
26  pthread_join(t[0], NULL);
27  pthread_join(t[1], NULL);
28  printf("OK\n");
29}
30
31// CHECK-NOT: WARNING: ThreadSanitizer: data race
32
33
34