>> So, let’s start with a much more correct premise: under NO conditions,
> for ANY reason,
> should a thread be suspended.
…
> The existence of a call does not mean you should use it.
Well, as it normally happens with absolute statements that allow no
compromise, this one is simply wrong…
Remember my policy on statements like this: newbies need a very simple set
of rules to follow to keep them from getting into trouble. Give them
rules that have too many qualifying clauses, and they will interpret one
of those clauses as covering the case they want, and go ahead and do it.
Things that you, or I, or Peter can do because we well-and-truly
understand Windows at a deep level, will only get a newbie into an
unrecoverable situation. One of the problems of teaching is making sure
beginners do not blow themselves up, in some cases, literally. Someone
who asks how to use SuspendThread is probably not qualified to use it.
For example, it is a good idea to tell a student pilot “Do not attempt a
takeoff when the crosswinds exceed 20 knots”. At some point, when the
student has enough hours in, the instructor will expose him/her to 30-knot
crosswinds. I was not taught spin recovery until I had enough hours in
that the instructor trusted my ability to get out of it without ripping
the wings off the plane.
“A superior pilot is someone who uses his superior judgment to avoid
getting into situations in which his superior skill is required for
survival”
You don’t take a 5-hour pilot and let him/her take off under IFR
(Instrument Flight Rules) conditions. A Navy pilot does not make his/her
first landing on a carrier. Your first exercise in the gym is not doing a
500-pound bench press (my triumph last week was moving from 4lb to 5lb
weights; a friend of mine the next station over was doing the same
exercise with 55lb weights)
Object lesson: a friend called me one night and said, “Did you hear about
JFK, Jr?” “No…” “He offed himself” “???” “Yes, with under 100 hours, he
flew over the ocean at night”. Yep. Because his instructor never gave
him the simple rule “Do not fly over the ocean at night” Lots of pilots
fly over the ocean at night. But those that survive didn’t do it with
less than 100 hours’ experience. So I learned to always give simple,
uncompromising rules. By the time the person recognizes the simplistic
nature of the rule, there’s enough background to know how to deal with it.
“Physics is taught by an ever-more-refined series of lies”. We teach
simple Newtonian mechanics. We progress through classic electromagnetism,
classic nuclear physics, special relativity, general relativity, quantum
physics, quantum electrodynamics, …
At each level, the student is told why the “truth” is not really the
truth, but the truth /really/ is…
My undergraduate major was math, with a minor in physics, but the year I
graduated the school decided to stop offering minot degrees on the
documents. About a decade ago, I read an introductory text on QED, and
when I was done, my reaction was “OMG! Physics is far simpler than what I
studied!” I worked two summers in a metallurgy research lab. I know how
I was taught math, chemistry, and physics. For some reason, we do not
have the same structure to software teaching. I’m trying to bring that
structure back.
SuspendThread is a very, very dangerous call. It should not be used in a
casual fashion, and certainly not by someone trying to implement such a
p-baked solution.
You can pull out all the weird exception cases and claim that my “absolute
rule” is wrong, but actually, these exotic cases merely strengthen my
point. Kids, don’t try this at hone.
[I once bought a book on “science experiments to do at home”, and one of
the project involved using red potassium, a chemical so toxic that 0.2mg
is the adult lethal dosage (not LD50, the dosage with 50% lethality, but
LD100). SuspendThread has many of the same properties]
joe
For example, consider writing a debugger - a thread created by debuggee
app has to be under the control of the debugger process, right. This is
where calls like SuspendThread() and SetThreadContext() come in handy.
Having said that, I have to make it clear that functions like
SuspendThread() and SetThreadContext() are NOT
meant for the general-purpose use, and, indeed, have a hight potential for
causing havoc if used improperly.
For example, consider what happens if you suspend a thread at he moment it
owns a critical section…
Yep, that’s the simple scenario. Even then, most of my students have
trouble because they’ve never seen concurrency locks until my course.
I’ve had to sometimes get two students to “act out” the deadlock scenario.
And it’s the simplest example. Recursive lock acquisition is another
thing they have trouble dealing with.
After 35 years of teaching software, I’ve learned a lot about how to keep
it simple.
joe
Anton Bassov
NTDEV is sponsored by OSR
Visit the list at: http://www.osronline.com/showlists.cfm?list=ntdev
OSR is HIRING!! See http://www.osr.com/careers
For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars
To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer