Re: how does system determine when to enter the thread function when using mutex
"Daniel" <newsonly@cableone.net> wrote in message
news:u5D9jXY7IHA.2072@TK2MSFTNGP04.phx.gbl
I was running the code in debug mode. I followed the sequence of the
code using the printf statements. In the other examples with
multiple threads the code execution would only change threads during
one of the "WaitForMessage" API functions
If it happened, it was just by accident. Don't extrapolate your
observations in a particular case to a general rule. Unless explicitly
synchronized, threads can run in any order, interleaved, and on
multi-CPU machines truly simultaneously.
This example would change threads right in the
middle of a set of statements and not during a "WaitForMessage"
function.
There's nothing wrong with that. printf statements that are outside the
section of code protected by the mutex can and will interleave. You use
a mutex precisely to prevent that. Note how the statements that are
inside the mutex are never interleaved:
printf("%d ---switch---\n", ...);
printf("%d writing to database...\n", ...);
You should never see "switch" message from one thread followed by
"switch" from another, without an intervening "writing to database" from
the first.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925