blob: 87731f5b2613e7556ffae56c28f2bf209ce02649 [file] [log] [blame]
bart178cc162008-05-10 12:52:02 +00001/** Multithreaded test program that triggers various access patterns without
2 * triggering any race conditions.
3 */
4
5
bart33f1c962008-05-10 13:17:34 +00006#define _GNU_SOURCE 1
7
bart178cc162008-05-10 12:52:02 +00008#include <pthread.h>
9#include <stdio.h>
10
11
12static pthread_rwlock_t s_rwlock;
13static int s_counter;
14
15static void* thread_func(void* arg)
16{
17 int i;
18 int sum = 0;
19
20 for (i = 0; i < 1000; i++)
21 {
22 pthread_rwlock_rdlock(&s_rwlock);
23 sum += s_counter;
24 pthread_rwlock_unlock(&s_rwlock);
25 pthread_rwlock_wrlock(&s_rwlock);
26 s_counter++;
27 pthread_rwlock_unlock(&s_rwlock);
28 }
29
30 return 0;
31}
32
33int main(int argc, char** argv)
34{
35 const int thread_count = 10;
36 pthread_t tid[thread_count];
37 int i;
38
39 for (i = 0; i < thread_count; i++)
40 {
41 pthread_create(&tid[i], 0, thread_func, 0);
42 }
43
44 for (i = 0; i < thread_count; i++)
45 {
46 pthread_join(tid[i], 0);
47 }
48
49 fprintf(stderr, "Finished.\n");
50
51 return 0;
52}