blob: 65c83efcc91c3fc6090e0e204cc4eed7dc8599ee [file] [log] [blame]
sewardjb4112022007-11-09 22:49:28 +00001
2#include <pthread.h>
3#include <stdio.h>
4#include <stdlib.h>
5
6/* Simple test program, has two races. A happens-before detector can only
7 ever detect one of them, though. */
8
9int unprot1 = 0, unprot2 = 0, prot = 0;
10pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER;
11
12void* child_fn ( void* arg )
13{
14 unprot1 ++;
15 pthread_mutex_lock( &mu );
16 prot ++;
17 pthread_mutex_unlock( &mu );
18 unprot2 ++;
19 return NULL;
20}
21
22int main ( void )
23{
24 pthread_t child;
25
26 if (pthread_create(&child, NULL, child_fn, NULL)) {
27 perror("pthread_create");
28 exit(1);
29 }
30
31 unprot1 ++;
32 pthread_mutex_lock( &mu );
33 prot ++;
34 pthread_mutex_unlock( &mu );
35 unprot2 ++;
36
37 if (pthread_join(child, NULL)) {
38 perror("pthread join");
39 exit(1);
40 }
41
42 return 0;
43}