Semaphore with timeout

  • Follow


Hi group

I have to port some Windows code which waits on a semaphore with a 
timeout.  I need to reliably determine whether the wait completed in the 
event of a timeout.  I can use System V or Posix semaphores.

I'm thinking about creating another thread and having it do a 
pthread_cond_timedwait on a condition which is never signalled.  This 
thread then interrupts the thread waiting on the semaphore with 
pthread_kill.  Testing for EINTR seems to eliminate the race condition 
between the timeout and the wait completing.

Does this work?  Am I overlooking a nicer solution, perhaps with proper 
use of condition variables?

Thank you


Sean
0
Reply seanf 7/17/2006 5:40:19 PM

seanf wrote:

> I have to port some Windows code which waits on a semaphore with a 
> timeout.  

Duh.  I should have said that I'm writing part of a library, so I don't 
want to interfere with my callers' use of alarm signals.
0
Reply seanf 7/17/2006 7:53:53 PM


On Mon, 17 Jul 2006 19:53:53 +0000, seanf wrote:

> seanf wrote:
> 
>> I have to port some Windows code which waits on a semaphore with a
>> timeout.
> 
> Duh.  I should have said that I'm writing part of a library, so I don't
> want to interfere with my callers' use of alarm signals.

You still need to contract the use of some signal. You wouldn't want to
deliver a signal w/ a default handler that killed the process.

Granted some applications overuse SIGALRM, so maybe SIGUSR1.


0
Reply William 7/17/2006 10:12:08 PM

seanf wrote:

> I have to port some Windows code which waits on a semaphore with a
> timeout.  I need to reliably determine whether the wait completed in the
> event of a timeout.  I can use System V or Posix semaphores.
>
> I'm thinking about creating another thread and having it do a
> pthread_cond_timedwait on a condition which is never signalled.  This
> thread then interrupts the thread waiting on the semaphore with
> pthread_kill.  Testing for EINTR seems to eliminate the race condition
> between the timeout and the wait completing.
>
> Does this work?  Am I overlooking a nicer solution, perhaps with proper
> use of condition variables?

What's the hard part? Why not just code a semaphore with exactly the
semantics you want using a mutex, a condition variable, and a predicate
integer?

DS

0
Reply davids 7/18/2006 12:32:29 PM

davids@webmaster.com wrote:

> seanf wrote:

>> I have to port some Windows code which waits on a semaphore with a
>> timeout.  I need to reliably determine whether the wait completed in the
>> event of a timeout.  I can use System V or Posix semaphores.

> What's the hard part? Why not just code a semaphore with exactly the
> semantics you want using a mutex, a condition variable, and a predicate
> integer?

It needs to be a real semaphore, i.e. for IPC.
0
Reply seanf 7/18/2006 5:34:30 PM

seanf wrote:
> Hi group
> 
> I have to port some Windows code which waits on a semaphore with a 
> timeout.  I need to reliably determine whether the wait completed in the 
> event of a timeout.  I can use System V or Posix semaphores.

I think I have a non-intrusive solution in the case where only there is 
only one waiter on the semaphore, which happily is my case.

Sorry to post pseudo code, but it's bed time.  Spurious wakeups, EINTR, 
etc, not shown.


main thread:

timeout = false
start secondary thread
result = sem_wait(blocking)
pthread_mutex_lock
pthread_cond_signal
pthread_mutex_unlock
if (timeout) {
     result = sem_wait(nonblocking)
}


secondary thread:

pthread_mutex_lock
pthread_cond_timedwait
if (ETIMEDOUT) {
     timeout = true
     sem_post
}
pthread_mutex_unlock
	
0
Reply seanf 7/18/2006 11:03:35 PM

5 Replies
420 Views

(page loaded in 0.099 seconds)

Similiar Articles:













7/23/2012 7:53:12 AM


Reply: