Re: How to wait for multiple threads simultaneously?
- From: Rainer Weikusat <rweikusat@xxxxxxxxxxx>
- Date: Wed, 13 Feb 2008 10:51:40 +0100
David Schwartz <davids@xxxxxxxxxxxxx> writes:
[...]
An even harder question, how can I wait for one thread's end and one
file descriptor(e.g STDIN_FILENO) to be readable simultaneously? -- If
either one of the two conditions becomes fulfilled, the other one must
quit its waiting status immediately as if no wait operation was
carried
out.
Have one thread wait for the thread's end and one thread wait for the
file descriptor. Have those threads signal a condition variable if the
event they are waiting for occurs.
If there was more than one thread to wait for, this approach
would double the amount of threads for no particular reason. Assuming
the reason to wait for the thread to terminate is not 'trying to
plaster over programming errors', ie 'restart it if it has crashed
again', causing each thread to inform 'some listener' that it has
stopped to work on whatever its task happened to be would be more
sensible. A simple way to do this would be to write the thread id to a
pipe immediatly before thread termination (at the expense of going
through the kernel for inter-thread communication). Using a
shared-memory message queue transporting 'event notifications', like
I/O-readiness of some file descriptor or termination of some thread
would be another option.
You can then spin on that condition variable.
To 'spin' on some lock would mean to busy-wait until it is free, eg
while (try_to_take(&lock) == FAILED);
.
- Follow-Ups:
- Re: How to wait for multiple threads simultaneously?
- From: David Schwartz
- Re: How to wait for multiple threads simultaneously?
- References:
- How to wait for multiple threads simultaneously?
- From: chjfth
- Re: How to wait for multiple threads simultaneously?
- From: David Schwartz
- How to wait for multiple threads simultaneously?
- Prev by Date: Re: I can't find gdk source tarballs and am serious pissed
- Next by Date: [Commercial] Training on Linux System and Kernel Programming from Professionals
- Previous by thread: Re: How to wait for multiple threads simultaneously?
- Next by thread: Re: How to wait for multiple threads simultaneously?
- Index(es):
Relevant Pages
|