49 lines · cpp
1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s2#include <pthread.h>3#include <stdlib.h>4#include <stdio.h>5#include "test.h"6 7void *Thread1(void *p) {8 *(int*)p = 42;9 return 0;10}11 12void *Thread2(void *p) {13 *(int*)p = 44;14 return 0;15}16 17__attribute__((noinline)) void *alloc() {18 return malloc(99);19}20 21void *AllocThread(void* arg) {22 return alloc();23}24 25int main() {26 void *p = 0;27 pthread_t t[2];28 pthread_create(&t[0], 0, AllocThread, 0);29 pthread_join(t[0], &p);30 print_address("addr=", 1, p);31 pthread_create(&t[0], 0, Thread1, (char*)p + 16);32 pthread_create(&t[1], 0, Thread2, (char*)p + 16);33 pthread_join(t[0], 0);34 pthread_join(t[1], 0);35 return 0;36}37 38// CHECK: addr=[[ADDR:0x[0-9,a-f]+]]39// CHECK: WARNING: ThreadSanitizer: data race40// ...41// CHECK: Location is heap block of size 99 at [[ADDR]] allocated by thread T1:42// CHECK: #0 malloc43// CHECK: #{{1|2}} alloc44// CHECK: #{{2|3}} AllocThread45// ...46// CHECK: Thread T1 (tid={{.*}}, finished) created by main thread at:47// CHECK: #0 pthread_create48// CHECK: #1 main49