1 /* Test data race detection between floating point variables. */
5 #include <stdio.h> /* printf() */
7 #include <unistd.h> /* sleep() */
10 /* Local functions declarations. */
12 static void* thread_func(void*);
15 /* Local variables. */
17 /* s_mutex protects s_d3. */
18 static pthread_mutex_t s_mutex
;
20 static double s_d1
; /* accessed before thread creation and in the created */
21 /* thread (not a race). */
22 static double s_d2
; /* accessed in the created thread and after the join */
24 static double s_d3
; /* accessed simultaneously from both threads (race). */
25 static int s_debug
= 0;
26 static int s_do_printf
= 0;
27 static int s_use_mutex
= 0;
30 /* Function definitions. */
32 int main(int argc
, char** argv
)
37 while ((optchar
= getopt(argc
, argv
, "dmp")) != EOF
)
55 pthread_mutex_init(&s_mutex
, 0);
58 * Switch to line-buffered mode, such that timing information can be
59 * obtained for each printf() call with strace.
65 printf("&s_d1 = %p; &s_d2 = %p; &s_d3 = %p\n", &s_d1
, &s_d2
, &s_d3
);
71 pthread_create(&threadid
, 0, thread_func
, 0);
73 sleep(1); /* Wait until thread_func() finished. */
76 if (s_use_mutex
) pthread_mutex_lock(&s_mutex
);
78 if (s_use_mutex
) pthread_mutex_unlock(&s_mutex
);
81 /* Wait until the thread finished. */
82 pthread_join(threadid
, 0);
83 if (s_do_printf
) printf("s_d2 = %g (should be 2)\n", s_d2
);
84 if (s_do_printf
) printf("s_d3 = %g (should be 5)\n", s_d3
);
86 pthread_mutex_destroy(&s_mutex
);
91 static void* thread_func(void* thread_arg
)
95 printf("s_d1 = %g (should be 1)\n", s_d1
);
99 if (s_use_mutex
) pthread_mutex_lock(&s_mutex
);
101 if (s_use_mutex
) pthread_mutex_unlock(&s_mutex
);