Usenet comp.unix.tru64 (2000-2013)
 help / color / mirror / Atom feed
* awaking a thread ... waiting on select or poll
@ 2001-04-13  1:34 MrSignor
  2001-04-13  3:21 ` Richard D. Latham
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: MrSignor @ 2001-04-13  1:34 UTC (permalink / raw)


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.

TIA

-Fred




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: awaking a thread ... waiting on select or poll
  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
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Richard D. Latham @ 2001-04-13  3:21 UTC (permalink / raw)


"MrSignor" <MrSignor@nospam_bellatlantic.net> writes:

> 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.
> 

Well, I alsmot hesitate to mention this because threads and signals
are like oil and water ... they don't mix very well :-)

But IIRC, you could use SIGUSR1 or SIGUSR2 to wake your other thread
out of a blocking select() or recv().

Note that several of the "middleware" products , notably MQ Series,
use threads and signals themselves ... and you have to play by
their rules if you're linking those libaries into your executable.

Hope this helps.

-- 
#include  <disclaimer.std>    /* I don't speak for IBM ...           */
                              /* Heck, I don't even speak for myself */
                              /* Don't believe me ? Ask my wife :-)  */
Richard D. Latham   lathamr@us.ibm.com


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: awaking a thread ... waiting on select or poll
  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
  2001-04-13 16:25 ` Logan Shaw
  2001-04-13 19:41 ` Norman Black
  3 siblings, 0 replies; 6+ messages in thread
From: Dave Butenhof @ 2001-04-13 10:45 UTC (permalink / raw)


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 ]-----/



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: awaking a thread ... waiting on select or poll
  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
@ 2001-04-13 16:25 ` Logan Shaw
  2001-05-29 17:29   ` anaonymous
  2001-04-13 19:41 ` Norman Black
  3 siblings, 1 reply; 6+ messages in thread
From: Logan Shaw @ 2001-04-13 16:25 UTC (permalink / raw)


In article <qIsB6.3628$zv6.2622115@typhoon2.ba-dsg.net>,
MrSignor <MrSignor@nospam_bellatlantic.net> 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.

Um...  Maybe I'm missing something obvious, but why not write to one of
the sockets it's listening to?

Perhaps you could use a pipe.  When you want the thread to wake up and
do whatever, just write one byte to the pipe, using a non-blocking
write.  If you succeed in writing, the poll()-ing/select()-ing process
will get the byte.  If you don't succeed in writing, that means another
byte is already in the pipe waiting to be read, which is just as good
anyway, since the bytes are just random garbage designed to cause the
thread to wake up.

  - Logan
-- 
my  your   his  her   our   their   _its_
I'm you're he's she's we're they're _it's_


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: awaking a thread ... waiting on select or poll
  2001-04-13  1:34 awaking a thread ... waiting on select or poll MrSignor
                   ` (2 preceding siblings ...)
  2001-04-13 16:25 ` Logan Shaw
@ 2001-04-13 19:41 ` Norman Black
  3 siblings, 0 replies; 6+ messages in thread
From: Norman Black @ 2001-04-13 19:41 UTC (permalink / raw)


You could create a pipe that you use only for manually waking a thread and
have that in select/poll.

Contrary to another message posed here, the Linux threads implementation
does not use SIGUSR1, at least not 2.x versions. The LinuxThreads
implementation uses realtime signals these days. I do not know when the
transition occurred.

--
Norman Black
Stony Brook Software
the reply, fubar => ix.netcom

"MrSignor" <MrSignor@nospam_bellatlantic.net> wrote in message
news:qIsB6.3628$zv6.2622115@typhoon2.ba-dsg.net...
> 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.
>
> TIA
>
> -Fred
>
>



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: awaking a thread ... waiting on select or poll
  2001-04-13 16:25 ` Logan Shaw
@ 2001-05-29 17:29   ` anaonymous
  0 siblings, 0 replies; 6+ messages in thread
From: anaonymous @ 2001-05-29 17:29 UTC (permalink / raw)



perhaps an even more basic idea is to have the select include a fd for some
open temp file, and the cooperating process/thread could then periodically
write something to this file forcing the blocked thread to "awaken?"

Logan Shaw wrote:

> In article <qIsB6.3628$zv6.2622115@typhoon2.ba-dsg.net>,
> MrSignor <MrSignor@nospam_bellatlantic.net> 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.
>
> Um...  Maybe I'm missing something obvious, but why not write to one of
> the sockets it's listening to?
>
> Perhaps you could use a pipe.  When you want the thread to wake up and
> do whatever, just write one byte to the pipe, using a non-blocking
> write.  If you succeed in writing, the poll()-ing/select()-ing process
> will get the byte.  If you don't succeed in writing, that means another
> byte is already in the pipe waiting to be read, which is just as good
> anyway, since the bytes are just random garbage designed to cause the
> thread to wake up.
>
>   - Logan
> --
> my  your   his  her   our   their   _its_
> I'm you're he's she's we're they're _it's_



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2001-05-29 17:29 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2001-04-13 16:25 ` Logan Shaw
2001-05-29 17:29   ` anaonymous
2001-04-13 19:41 ` Norman Black

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