Home > Archive > Unix Programming > July 2006 > Semaphore with timeout
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Semaphore with timeout
|
|
|
| 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
| |
| William Ahern 2006-07-17, 7:00 pm |
| On Mon, 17 Jul 2006 19:53:53 +0000, seanf wrote:
> seanf wrote:
>
>
> 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.
| |
| davids@webmaster.com 2006-07-18, 7:59 am |
|
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
| |
|
| davids@webmaster.com wrote:
> seanf wrote:
[color=darkred]
> 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.
| |
|
| 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
|
|
|
|
|