Add exit condition.

Requires adding a counter to the queue. If the last active thread was
going to also wait on the condition variable, we know we can quit.
This commit is contained in:
Érico Nogueira 2021-09-18 15:47:06 -03:00
parent 82d06ef184
commit 1d315663ba
1 changed files with 8 additions and 1 deletions

View File

@ -31,7 +31,8 @@ struct queue {
pthread_cond_t cond;
size_t len, size;
struct task *tasks;
/* add a counter to be decremented by each thread that can't add more stuff until we know we can stop searching? */
/* number of free threads */
long free;
};
static struct queue queue = {0};
@ -73,7 +74,13 @@ int queue_remove(struct queue *q, struct task *t)
int rv = 0;
pthread_mutex_lock(&q->mtx);
while (q->len == 0) {
if (q->free == nproc - 1) {
/* we are done removing things */
exit(0);
}
q->free++;
pthread_cond_wait(&q->cond, &q->mtx);
q->free--;
}
if (q->len == 0) {
rv = EAGAIN;