blob: c094b115a09087d1a12db06a4e9a86fedcb18497 [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
6#include <pthread.h>
7#include <stdio.h>
8
9
10static pthread_rwlock_t s_rwlock;
11static int s_counter;
12
13static void* thread_func(void* arg)
14{
15 int i;
16 int sum = 0;
17
18 for (i = 0; i < 1000; i++)
19 {
20 pthread_rwlock_rdlock(&s_rwlock);
21 sum += s_counter;
22 pthread_rwlock_unlock(&s_rwlock);
23 pthread_rwlock_wrlock(&s_rwlock);
24 s_counter++;
25 pthread_rwlock_unlock(&s_rwlock);
26 }
27
28 return 0;
29}
30
31int main(int argc, char** argv)
32{
33 const int thread_count = 10;
34 pthread_t tid[thread_count];
35 int i;
36
37 for (i = 0; i < thread_count; i++)
38 {
39 pthread_create(&tid[i], 0, thread_func, 0);
40 }
41
42 for (i = 0; i < thread_count; i++)
43 {
44 pthread_join(tid[i], 0);
45 }
46
47 fprintf(stderr, "Finished.\n");
48
49 return 0;
50}