A. Bolmarcich wrote:
On 2007-02-01, Knute Johnson <nospam@rabbitbrush.frazmtn.com> wrote:
A. Bolmarcich wrote:
Like many questions about the interaction of multiple threads, the
question does not have a simple "yes" or "no" answer. The best that I
can respond is to repeat: a read is subsequent to a write that wrote the
value that was read. If that is what you mean by "subsequent read" in
your qestion, the answer is "yes".
I have no idea what that means. By subsequent I mean the usual meaning
that the subsequent action occurs later in time than the precedent
action. So in the case I am asking about, the first thread writes to
the variable and then some time later the second thread reads the variable.
Does volatile guarantee that the second thread will see the value
written by the first thread?
Does synchronizing guarantee that the second thread will see the value
written by the first thread?
When "the first thread writes to the variable" a processor requests that
a value be written to a memory address. On some computers the request
goes into a queue and the effect of the memory write is visible to other
threads sometime later. While the memory write is being done, the first
thread continues executing.
On this type of computer architecture, whether a memory read is subsequent
to a memory write depends on the whether the read gets the value written
by the memory write. It does not depend on the global order in which
threads execute instructions.
The answer to your question: "Does volatile guarantee that the second
thread will see the value written by the first thread?", is yes, as long
as you take "some time later" being in terms of what is visible in memory
and not in terms of the global order in which the threads execute the
instruction.
The answer to your question: "Does synchronizing guarantee that the
second thread will see the value written by the first thread?" is yes,
with "some time later" being in terms of the second thread entering its
synchronized block after the first thread exited its synchronized block.
Thanks very much. I appreciate the time and effort.