Use semaphore for producer/consumer case...

Stelian Pop (stelian.pop@fr.alcove.com)
Fri, 23 Mar 2001 13:07:22 +0100


Hi,

I want to use a semaphore for the classic producer/consumer case
(put the consumer to wait until X items are produced, where X != 1).

If X is 1, the semaphore is a simple MUTEX, ok.

But if the consumer wants to wait for several items, it doesn't
seem to work (or something is bad in my code).

What is wrong in the following ?

DECLARE_MUTEX(sem);

producer() {
/* One item produced */
up(&sem);
}

consumer() {
/* Let's wait for 10 items */
atomic_set(&sem->count, -10);

/* This starts the producers, they will call producer()
some time in the future */
start_producers();

/* Wait for completion */
down(&sem);
}

I can get around this by using a mutex and a separate atomic_t
'count' variable, but it's more a workaround that a solution...

Thanks,

Stelian.

-- 
Stelian Pop <stelian.pop@fr.alcove.com>
|------------- Ingénieur Informatique Libre --------------|
| Alcôve - http://www.alcove.com - Tel: +33 1 49 22 68 00 |
|----------- Alcôve, l'informatique est libre ------------|
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/