Home | History | Annotate | Download | only in tsan
      1 // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
      2 #include <pthread.h>
      3 #include <stddef.h>
      4 #include <stdio.h>
      5 #include <unistd.h>
      6 
      7 void *Thread1(void *x) {
      8   int *p = (int*)x;
      9   p[0] = 1;
     10   return NULL;
     11 }
     12 
     13 void *Thread2(void *x) {
     14   sleep(1);
     15   char *p = (char*)x;
     16   p[2] = 1;
     17   return NULL;
     18 }
     19 
     20 int main() {
     21   int *data = new int(42);
     22   fprintf(stderr, "ptr1=%p\n", data);
     23   fprintf(stderr, "ptr2=%p\n", (char*)data + 2);
     24   pthread_t t[2];
     25   pthread_create(&t[0], NULL, Thread1, data);
     26   pthread_create(&t[1], NULL, Thread2, data);
     27   pthread_join(t[0], NULL);
     28   pthread_join(t[1], NULL);
     29   delete data;
     30 }
     31 
     32 // CHECK: ptr1=[[PTR1:0x[0-9,a-f]+]]
     33 // CHECK: ptr2=[[PTR2:0x[0-9,a-f]+]]
     34 // CHECK: WARNING: ThreadSanitizer: data race
     35 // CHECK:   Write of size 1 at [[PTR2]] by thread T2:
     36 // CHECK:   Previous write of size 4 at [[PTR1]] by thread T1:
     37