...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <threads.h>
#include <stdio.h>
void *run_step(void *t) {
static int current_step = 0;
int my_step = (int)t;
int result;
if ((result = mtx_lock(&mutex)) != thrd_success) {
/* 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 ((result = cnd_wait(&cond, &mutex)) != thrd_success) {
/* 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 ((result = cnd_broadcast(&cond)) != thrd_success) {
/* Handle error condition */
}
printf("Thread %d is exiting...\n", my_step);
if ((result = mtx_unlock(&mutex)) != 0) {
/* Handle error condition */
}
thrd_exit(NULL);
}
|
...