...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stdio.h>
#include <threads.h>
int run_step(void *t) {
static int current_step = 0;
int my_step = (int)t;
if (thrd_success != mtx_lock(&mutex)) {
/* Handle error condition. */
}
printf("Thread %d has the lock\n", my_step);
while (current_step != my_step) {
printf("Thread %d is sleeping...\n", my_step);
if (thrd_success != cnd_wait(&cond, &mutex)) {
/* Handle error condition. */
}
printf("Thread %d woke up\n", my_step);
}
/* Do processing... */
printf("Thread %d is processing...\n", my_step);
current_step++;
/* Signal ALL waiting tasks. */
if (thrd_success != cnd_broadcast(&cond)) {
/* Handle error condition */
}
printf("Thread %d is exiting...\n", my_step);
if (thrd_success != mtx_unlock(&mutex)) != 0) {
/* Handle error condition */
}
return 0;
}
|
...