1 #include <pthread.h> 2 #include <stddef.h> 3 #include <stdio.h> 4 #include <string.h> 5 #include <unistd.h> 6 7 char *data = new char[10]; 8 char *data1 = new char[10]; 9 char *data2 = new char[10]; 10 11 void *Thread1(void *x) { 12 memcpy(data+5, data1, 1); 13 return NULL; 14 } 15 16 void *Thread2(void *x) { 17 usleep(500*1000); 18 memcpy(data+3, data2, 4); 19 return NULL; 20 } 21 22 int main() { 23 fprintf(stderr, "addr=%p\n", &data[5]); 24 pthread_t t[2]; 25 pthread_create(&t[0], NULL, Thread1, NULL); 26 pthread_create(&t[1], NULL, Thread2, NULL); 27 pthread_join(t[0], NULL); 28 pthread_join(t[1], NULL); 29 return 0; 30 } 31 32 // CHECK: addr=[[ADDR:0x[0-9,a-f]+]] 33 // CHECK: WARNING: ThreadSanitizer: data race 34 // CHECK: Write of size 1 at [[ADDR]] by thread 2: 35 // CHECK: #0 memcpy 36 // CHECK: #1 Thread2 37 // CHECK: Previous write of size 1 at [[ADDR]] by thread 1: 38 // CHECK: #0 memcpy 39 // CHECK: #1 Thread1 40 41