Debian Codegraph @

gcc/testsuite/g++.dg/tsan/fd_close_norace.C

raw
1 #include <pthread.h>
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7
8 void *Thread1(void *x) {
9 int f = open("/dev/random", O_RDONLY);
10 close(f);
11 return NULL;
12 }
13
14 void *Thread2(void *x) {
15 sleep(1);
16 int f = open("/dev/random", O_RDONLY);
17 close(f);
18 return NULL;
19 }
20
21 int main() {
22 pthread_t t[2];
23 pthread_create(&t[0], NULL, Thread1, NULL);
24 pthread_create(&t[1], NULL, Thread2, NULL);
25 pthread_join(t[0], NULL);
26 pthread_join(t[1], NULL);
27 fprintf(stderr, "OK\n");
28 }
29
30 /* { dg-prune-output "WARNING: ThreadSanitizer: data race.*(\n|\r\n|\r)" } */