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 | |
| 6 | #include <pthread.h> |
| 7 | #include <stdio.h> |
| 8 | |
| 9 | |
| 10 | static pthread_rwlock_t s_rwlock; |
| 11 | static int s_counter; |
| 12 | |
| 13 | static 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 | |
| 31 | int 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 | } |