mirror of https://github.com/ericonr/erm.git
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:
parent
82d06ef184
commit
1d315663ba
9
remove.c
9
remove.c
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue