Usenet comp.unix.tru64 (2000-2013)
 help / color / mirror / Atom feed
From: Dave Butenhof <David.Butenhof@compaq.com>
Subject: Re: awaking a thread ... waiting on select or poll
Date: Fri, 13 Apr 2001 06:45:22 -0400	[thread overview]
Message-ID: <3AD6D8C2.62C0C704@compaq.com> (raw)
In-Reply-To: qIsB6.3628$zv6.2622115@typhoon2.ba-dsg.net

MrSignor wrote:

> Is there a reliable way (across Unix platforms) to wake-up a thread
> sleeping/waiting on a Select or Poll, that is not timing out the request.
>
> Basically, I have a thread, waiting for work on 1-n sockets. Upon
> occassion, I would like to wake the sleeping-thread, because I
> need to bring something to it's attention.

Given "pthread_t Target;", use "pthread_kill(Target,SIGUSR1)"... or whatever
other signal seems appropriate. (Beware that on Linux, the thread
implementation uses SIGUSR1 itself.) Use a signal that you can distinguish
from "real signals". (E.g., don't use SIGSEGV, or SIGCHLD.) Of course, you
need to know the pthread_t value of the thread. That must be recorded either
by the creator or the thread itself using pthread_self().

This'll work for any syscall that can return EINTR. The call site can of
course detect the interruption by looking for -1 and EINTR return. Normally,
the signal action for the chosen signal should be set as an empty routine
(just returns).

If more than one thread might want to do this, you might consider a "time
server" that allows individual threads to get alarm()-like behavior without
interfering with each other. You call some API that queues a wakeup request,
and a separate server thread zaps each registered thread at the appropriate
time. One example package is in the thread utilities library from Bil Lewis'
book Multithreaded Programming with Pthreads; his examples are available
online from http://www.LambdaCS.com.

/------------------[ David.Butenhof@compaq.com ]------------------\
| Compaq Computer Corporation              POSIX Thread Architect |
|     My book: http://www.awl.com/cseng/titles/0-201-63392-2/     |
\-----[ http://home.earthlink.net/~anneart/family/dave.html ]-----/



  parent reply	other threads:[~2001-04-13 10:45 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-04-13  1:34 awaking a thread ... waiting on select or poll MrSignor
2001-04-13  3:21 ` Richard D. Latham
2001-04-13 10:45 ` Dave Butenhof [this message]
2001-04-13 16:25 ` Logan Shaw
2001-05-29 17:29   ` anaonymous
2001-04-13 19:41 ` Norman Black
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox