You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apr.apache.org by Bruno Haas <gr...@laposte.net> on 2005/05/18 13:09:34 UTC

counting semaphores

I am quite surprised that there are no counting semaphores in apr. What 
I would need to do is have thread A releasing a semaphore for as many 
times as an event has happened and thread B blocked waiting for the 
semaphore. The counting semaphore would guarantee that thread B is 
"woken up" as many times as the event has happened.

I seem to be unable to do this sort of thing using only mutexes, any ideas ?

Bruno

Re: counting semaphores

Posted by Aaron Bannert <aa...@clove.org>.
Have you looked at the apr_thread_cond_* API? These are
virtually identical to POSIX Condition Variables, and can
be used to do what you're talking about.

-aaron


On May 18, 2005, at 4:09 AM, Bruno Haas wrote:

> I am quite surprised that there are no counting semaphores in apr.  
> What I would need to do is have thread A releasing a semaphore for  
> as many times as an event has happened and thread B blocked waiting  
> for the semaphore. The counting semaphore would guarantee that  
> thread B is "woken up" as many times as the event has happened.
>
> I seem to be unable to do this sort of thing using only mutexes,  
> any ideas ?
>
> Bruno
>
>


Re: counting semaphores

Posted by Jeff Trawick <tr...@gmail.com>.
On 5/18/05, Dan Johnson <dm...@nrtc.northrop.com> wrote:

> I also don't understand why semaphores are missing from APR, especially
> since mutexes seem to have been implemented on top of them in many
> cases. This must have been a deliberate choice, but why?

I would assume that nobody who needed them proposed a patch ;)

RE: counting semaphores

Posted by Dan Johnson <dm...@nrtc.northrop.com>.
I ran into the same problem and (since I was bound to using just APR)
wound up using atomic integer operations combined with a mutex.
Basically, a semaphore wait looks like:

if (apr_atomic_dec32 (&count) < 0)
	apr_thread_mutex_lock (mutex);

and a semaphore post looks like:

if ((apr_int32_t) apr_atomic_inc32 (&count) <= 0)
	apr_thread_mutex_unlock (mutex);

You can get the effect of cross-process semaphores by using
cross-process mutexes and putting your count variable in shared memory.

I also don't understand why semaphores are missing from APR, especially
since mutexes seem to have been implemented on top of them in many
cases. This must have been a deliberate choice, but why?

Dan


-----Original Message-----
From: Bruno Haas [mailto:grinob.haas@laposte.net] 
Sent: Wednesday, May 18, 2005 4:10 AM
To: dev@apr.apache.org
Subject: counting semaphores

I am quite surprised that there are no counting semaphores in apr. What 
I would need to do is have thread A releasing a semaphore for as many 
times as an event has happened and thread B blocked waiting for the 
semaphore. The counting semaphore would guarantee that thread B is 
"woken up" as many times as the event has happened.

I seem to be unable to do this sort of thing using only mutexes, any
ideas ?

Bruno