| 1 |
#include <pthread.h> |
| 2 |
#include <stdio.h> |
| 3 |
#include <string.h> |
| 4 |
#include <time.h> |
| 5 |
#include <unistd.h> |
| 6 |
#include <errno.h> |
| 7 |
|
| 8 |
pthread_mutex_t mutex; |
| 9 |
pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 10 |
pthread_rwlock_t rwlock; |
| 11 |
pthread_cond_t cond; |
| 12 |
|
| 13 |
static int do_output; |
| 14 |
|
| 15 |
static void output(char *string) |
| 16 |
{ |
| 17 |
if (!do_output) |
| 18 |
return; |
| 19 |
|
| 20 |
printf("%s", string); |
| 21 |
} |
| 22 |
|
| 23 |
void *thread_func(void *p) |
| 24 |
{ |
| 25 |
output("thread_func\n"); |
| 26 |
|
| 27 |
pthread_mutex_lock(&mutex); |
| 28 |
output("got mutex lock\n"); |
| 29 |
|
| 30 |
sleep(5); |
| 31 |
|
| 32 |
pthread_mutex_unlock(&mutex); |
| 33 |
|
| 34 |
output("thread done\n"); |
| 35 |
|
| 36 |
return NULL; |
| 37 |
} |
| 38 |
|
| 39 |
void *wrlock_thread(void *p) |
| 40 |
{ |
| 41 |
pthread_rwlock_rdlock(&rwlock); |
| 42 |
output("thread got read lock\n"); |
| 43 |
|
| 44 |
sleep(2); |
| 45 |
pthread_rwlock_unlock(&rwlock); |
| 46 |
|
| 47 |
pthread_rwlock_wrlock(&rwlock); |
| 48 |
output("thread got write lock\n"); |
| 49 |
|
| 50 |
return NULL; |
| 51 |
} |
| 52 |
|
| 53 |
void *condvar_thread(void *p) |
| 54 |
{ |
| 55 |
int i; |
| 56 |
|
| 57 |
pthread_mutex_lock(&count_mutex); |
| 58 |
|
| 59 |
for (i=0; i < 10; i++) |
| 60 |
{ |
| 61 |
output("Going for sleep for 1 sec\n"); |
| 62 |
sleep(1); |
| 63 |
} |
| 64 |
|
| 65 |
pthread_cond_signal(&cond); |
| 66 |
|
| 67 |
pthread_mutex_unlock(&count_mutex); |
| 68 |
|
| 69 |
return NULL; |
| 70 |
} |
| 71 |
|
| 72 |
void *condvar_wait_thread(void *p) |
| 73 |
{ |
| 74 |
pthread_mutex_lock(&count_mutex); |
| 75 |
pthread_cond_wait(&cond, &count_mutex); |
| 76 |
|
| 77 |
output("thread wakeup on cond var\n"); |
| 78 |
|
| 79 |
pthread_mutex_unlock(&count_mutex); |
| 80 |
|
| 81 |
return NULL; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
int main(int argc, char *argv[]) |
| 86 |
{ |
| 87 |
pthread_t tid, tid2; |
| 88 |
struct timespec abs_time; |
| 89 |
|
| 90 |
do_output = (argc > 1 && strcmp("-v", argv[1]) == 0); |
| 91 |
|
| 92 |
/* Simple mutex lock */ |
| 93 |
|
| 94 |
pthread_mutex_init(&mutex, NULL); |
| 95 |
|
| 96 |
pthread_mutex_lock(&mutex); |
| 97 |
|
| 98 |
pthread_create(&tid, NULL, thread_func, NULL); |
| 99 |
|
| 100 |
pthread_mutex_unlock(&mutex); |
| 101 |
|
| 102 |
sleep(1); |
| 103 |
|
| 104 |
while (1) |
| 105 |
{ |
| 106 |
int ret; |
| 107 |
|
| 108 |
clock_gettime(CLOCK_REALTIME, &abs_time); |
| 109 |
abs_time.tv_sec += 1; |
| 110 |
|
| 111 |
ret = pthread_mutex_timedlock(&mutex, &abs_time); |
| 112 |
|
| 113 |
if (ret == ETIMEDOUT) |
| 114 |
{ |
| 115 |
output("ETIMEDOUT\n"); |
| 116 |
} |
| 117 |
else if (ret == 0) |
| 118 |
{ |
| 119 |
output("pthread_mutex_timedlock succ.\n"); |
| 120 |
break; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
pthread_mutex_destroy(&mutex); |
| 125 |
|
| 126 |
pthread_join(tid, NULL); |
| 127 |
|
| 128 |
/* Read-Write lock */ |
| 129 |
|
| 130 |
pthread_rwlock_init(&rwlock, NULL); |
| 131 |
|
| 132 |
pthread_create(&tid, NULL, wrlock_thread, NULL); |
| 133 |
|
| 134 |
pthread_rwlock_rdlock(&rwlock); |
| 135 |
sleep(1); |
| 136 |
|
| 137 |
pthread_rwlock_unlock(&rwlock); |
| 138 |
|
| 139 |
pthread_join(tid, NULL); |
| 140 |
|
| 141 |
/* Condition Variable */ |
| 142 |
|
| 143 |
pthread_cond_init(&cond, NULL); |
| 144 |
pthread_mutex_lock(&count_mutex); |
| 145 |
|
| 146 |
pthread_create(&tid, NULL, condvar_thread, NULL); |
| 147 |
|
| 148 |
pthread_cond_wait(&cond, &count_mutex); |
| 149 |
|
| 150 |
|
| 151 |
pthread_cond_init(&cond, NULL); |
| 152 |
pthread_mutex_init(&count_mutex, NULL); |
| 153 |
|
| 154 |
pthread_mutex_lock(&count_mutex); |
| 155 |
|
| 156 |
pthread_create(&tid, NULL, condvar_thread, NULL); |
| 157 |
|
| 158 |
clock_gettime(CLOCK_REALTIME, &abs_time); |
| 159 |
abs_time.tv_sec += 1; |
| 160 |
|
| 161 |
if (pthread_cond_timedwait(&cond, &count_mutex, &abs_time) == ETIMEDOUT) |
| 162 |
output("cond_timedwait ETIMEDOUT\n"); |
| 163 |
|
| 164 |
/* main thread calls pthread_cond_broadcast() this time */ |
| 165 |
|
| 166 |
pthread_cond_init(&cond, NULL); |
| 167 |
pthread_mutex_init(&count_mutex, NULL); |
| 168 |
|
| 169 |
pthread_create(&tid, NULL, condvar_wait_thread, NULL); |
| 170 |
pthread_create(&tid2, NULL, condvar_wait_thread, NULL); |
| 171 |
|
| 172 |
sleep(1); |
| 173 |
|
| 174 |
pthread_mutex_lock(&count_mutex); |
| 175 |
|
| 176 |
pthread_cond_broadcast(&cond); |
| 177 |
|
| 178 |
pthread_mutex_unlock(&count_mutex); |
| 179 |
|
| 180 |
pthread_join(tid, NULL); |
| 181 |
pthread_join(tid2, NULL); |
| 182 |
|
| 183 |
return 0; |
| 184 |
} |