1
2 #include <pthread.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6
7 /* Test of the mechanism for showing all locks held by a thread -- one
8 thread has a lock, the other doesn't. Running w/ command line args
9 switches the has/has-not thread around, so as to test lockset
10 retention in both the history mechanism and the primary errors. */
11
12 pthread_mutex_t mx = PTHREAD_MUTEX_INITIALIZER;
13
14 int x = 0;
15
child_fn(void * arg)16 void* child_fn ( void* arg )
17 {
18 if (arg) pthread_mutex_lock(&mx);
19 x = 1;
20 if (arg) pthread_mutex_unlock(&mx);
21 return NULL;
22 }
23
main(int argc,char ** argv)24 int main ( int argc, char** argv )
25 {
26 int sw = argc > 1;
27 pthread_t child1, child2;
28
29 if (pthread_create(&child1, NULL, child_fn, (void*)(long)(sw ? 0 : 1))) {
30 perror("pthread_create1");
31 exit(1);
32 }
33 sleep(1); /* ensure repeatable results */
34 if (pthread_create(&child2, NULL, child_fn, (void*)(long)(sw ? 1 : 0))) {
35 perror("pthread_create1");
36 exit(1);
37 }
38
39 if (pthread_join(child1, NULL)) {
40 perror("pthread join1");
41 exit(1);
42 }
43
44 if (pthread_join(child2, NULL)) {
45 perror("pthread join2");
46 exit(1);
47 }
48
49 return 0;
50 }
51