lkml.org 
[lkml]   [2001]   [Mar]   [23]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectUse semaphore for producer/consumer case...
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/

\
 
 \ /
  Last update: 2005-03-22 13:17    [W:0.044 / U:0.212 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site