1 // RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2 #include <pthread.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <netinet/in.h>
9 #include <arpa/inet.h>
10 
11 struct sockaddr_in addr;
12 int X;
13 
ClientThread(void * x)14 void *ClientThread(void *x) {
15   int c = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
16   X = 42;
17   if (connect(c, (struct sockaddr*)&addr, sizeof(addr))) {
18     perror("connect");
19     exit(1);
20   }
21   close(c);
22   return NULL;
23 }
24 
main()25 int main() {
26   int s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
27   addr.sin_family = AF_INET;
28   inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
29   addr.sin_port = INADDR_ANY;
30   socklen_t len = sizeof(addr);
31   bind(s, (sockaddr*)&addr, len);
32   getsockname(s, (sockaddr*)&addr, &len);
33   listen(s, 10);
34   pthread_t t;
35   pthread_create(&t, 0, ClientThread, 0);
36   int c = accept(s, 0, 0);
37   X = 42;
38   pthread_join(t, 0);
39   close(c);
40   close(s);
41   printf("OK\n");
42 }
43 
44 // CHECK-NOT: WARNING: ThreadSanitizer: data race
45 
46