Question:
someone provided a cleaner version of the
susp.c in which functions for suspending and resume
a thread is defined. Please see reference below.
I compiled his updated example and it run fined. But
it find his solution troublesome. If anyone can clarify
that for me, I would be appreciated.
1) The suspend_init_routine() is initialized by pthread_once()
which is inside thr_suspend() and thr_continue().
In his example, thr_suspend() and thr_contine() are called
by the main thread. But since the worker threads are already
created. I am not sure whether their signal masks would be
updated accordingly?
2) Can a thread can thr_suspend() in order to suspend itself?
If it can and it's the first one to call thr_suspend() or
thr_continue(). With the signal masks of other threads
be updated accordingly?
3) Seems to me that after a specific thread is suspended and
resumed, suspending it again would not work.
Answer:
Yes, but it really doesn't have anything to do with timeslicing; for
example, on an SMP, both threads could be running simultanteously.
I've known for a long time that the susp.c program had bugs. (As I said
in
the footnote, it wasn't written by me. Unfortunately, since I chose to
publish it, I can't exactly say that absolves me of responsibility for
the
errors.
Anyway, one particularly major error is that the signal handler for
SIGUSR1
is set up incorrectly. The race can be resolved by setting sa_mask to
block
both SIGUSR1 and SIGUSR2 until the sigsuspend call unblocks SIGUSR2. In
practice, this doesn't happen easily or often. The susp.c program was
being
developed as a sample to show our Java team how to do Java's
suspend/resume
(especially for garbage collection). It was intended to be just a quick
sample to get them started. I was just finishing the book. I wasn't very
happy with the completely artificial and useless pthread_kill example
program I'd written, and I knew many people would be interested in
asynchronous suspend/resume (however bad an idea that may be!), so I
decided
to switch to susp.c. It "seemed to work" in initial testing, and I
didn't
have the time to try to completely rewrite it. (Though there were
several
aspects I'd have liked to change, including using semaphores for a more
reliable suspend synchronization mechanism.) Of course, the more
something
like that is used, the more the seams start splitting open, and it
wasn't
long before Java's attempts to actually use the mechanism started
showing
errors.
On and off, I've been working on a bunch of corrections for the next
printing. (And I'd like to find time to start on a second edition, but
that's another story.) I've been fixing some of the examples, and I did
a
substantial amount of work on susp.c. What I have now is a fixed but
only
partly "cleaned up" version to fix the worst of the problems without too
much change. I'll include the new code as an attachment.
By the way, if you've got a version of the book earlier than the 3rd
printing, you might want to take a look at the Errata list. I really
need
to
find time to figure out who to talk to about posting it on the awl web
site,
but, right now, you can find it at
http://members.aol.com/drbutenhof/Errata.html.