bart | 178cc16 | 2008-05-10 12:52:02 +0000 | [diff] [blame] | 1 | /** Multithreaded test program that triggers various access patterns without |
| 2 | * triggering any race conditions. |
| 3 | */ |
| 4 | |
| 5 | |
bart | 33f1c96 | 2008-05-10 13:17:34 +0000 | [diff] [blame] | 6 | #define _GNU_SOURCE 1 |
| 7 | |
bart | 178cc16 | 2008-05-10 12:52:02 +0000 | [diff] [blame] | 8 | #include <pthread.h> |
| 9 | #include <stdio.h> |
| 10 | |
| 11 | |
| 12 | static pthread_rwlock_t s_rwlock; |
| 13 | static int s_counter; |
| 14 | |
| 15 | static 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 | |
| 33 | int 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 | } |