|
|
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
|
|
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: Semaphore with timeout - comp.unix.programmerHi 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... Tar to another computer follow up: The semaphore timeout period ...Hi All, I promised you all I would let you know the next time I got out to that ancient Caldera UNIX server with the broken 4mm tape drive. Th... How to find semaphore ID from pid - comp.unix.solarisSemaphore with timeout - comp.unix.programmer Semaphore with timeout - comp.unix.programmer Finding the PID given a TCP connection - comp.unix.programmer ... semaphores in Solaris vs Linux - comp.unix.solarisSemaphore with timeout - comp.unix.programmer semaphores in Solaris vs Linux - comp.unix.solaris Semaphore with timeout - comp.unix.programmer Thread creation in a kernel ... Global Hook for Window Creation and Destruction - comp.lang.python ...Semaphore with timeout - comp.unix.programmer Global Hook for Window Creation and Destruction - comp.lang.python ... Semaphore with timeout - comp.unix.programmer Global ... Finding the PID given a TCP connection - comp.unix.programmer ...How to find semaphore ID from pid - comp.unix.solaris Finding the PID given a ... I currently read /proc/net/tcp, but > that is obviously linux ... Semaphore with timeout ... pthread_cond_wait problem - comp.unix.programmerSemaphore with timeout - comp.unix.programmer I'm thinking about creating another thread and having it do a pthread_cond_timedwait on a ... A quick search provides two ... ftp timeout - comp.unix.solarisTar to another computer follow up: The semaphore timeout period ... ftp timeout - comp.unix.solaris Tar to another computer follow up: The semaphore timeout period ... Protect a semaphore of crash in the critical area - comp.unix ...... handling of one of them is taken as a critical area, protected using UNIX semaphores ... and the other processes send requests to this process and include some timeout ... Watchdog thread syncronisation with semphores? - comp.unix ...a 'watchdog kicking' thread, which blocks on all semaphores that are given by the ... PauseBetweenSqueals);// usleep might be better, depends on the watchdog timeout? Unix & Linux: Semaphore with timeout - programming.itags.orgprogramming.itags.org: Unix & Linux question: Semaphore with timeout, created at:Wed, 07 May 2008 20:55:00 GMT with 692 bytes, last updated: Saturday, July 21, 2012 ... Semaphore with timeout - comp.unix.programmer | Computer GroupHi 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... 7/23/2012 7:53:12 AM
|
|
|
|
|
|
|
|
|