Re: Two more multithreading questions



A. Bolmarcich wrote:
On 2007-01-30, Knute Johnson <nospam@xxxxxxxxxxxxxxxxxxxxxxx> wrote:
Thank you very much for your response but it doesn't really answer my question. As to the immutable, I'm not muting. So let me set the scenario again.

Class with instance variable that is reference to Integer. One thread makes new Integers and assigns them to the instance variable. The other thread calls some method on the Integer. I want to know if making the variable volatile will guarantee that the second thread always sees the latest integer created by the first thread.

No, volatile does not "guarantee that the second thread always sees the
latest integer created by the first thread". What volatile guarantees
is that when one thread executes assignment statements to volatile
variables other threads will see the effect of the assignment
statements in the same order that they were executed.

It is possible that one thread starts executing an assignment statement
to a volatile variable and very slightly later another thread starts
executing a statement that uses the value of that variable. The second
thread may get the value of the variable before latest assignment
statment by the first thread. A basic problem is that without some
form of synchronization between theads (not necessarily by the Java
synchronize statement) there is no definite time ordering of operations
between threads.

This is why I keep asking these questions because I get different answers. Can you explain what is meant in the documentation then by:

JLS 17.4.4 Synchronization Order
....
A write to a volatile variable (§8.3.1.4) v synchronizes-with all subsequent reads of v by any thread (where subsequent is defined according to the synchronization order).

Thanks very much,

--

Knute Johnson
email s/nospam/knute/
.



Relevant Pages

  • Re: Two more multithreading questions
    ... volatile does not "guarantee that the second thread always sees the ... latest integer created by the first thread". ... is that when one thread executes assignment statements to volatile ... It is possible that one thread starts executing an assignment statement ...
    (comp.lang.java.programmer)
  • Re: Thread question
    ... the variables freely, without synchronization. ... I merely said your advice was incorrect. ... use volatile -- doing so unnecessarily slows down your programs, ... VOLATILE int total=0; ...
    (comp.os.linux.development.apps)
  • Re: Is the following code MT-Safe?
    ... the assert (which is a fundamental design error: ... and almost always leads to either major synchronization failures ... >Does _bRunning need to be tagged as volatile? ... compiler to cache values, but only during the execution of a function; ...
    (microsoft.public.vc.mfc)
  • Re: Share .cpp and .h along projects
    ... pvector = InterlockedExchangePointerAcquire(&g_sharedVector, ... I really would like to hear what you think volatile accomplishes ... You can't require people to use volatile on top of synchronization. ... compiler useful for multithreaded programming. ...
    (microsoft.public.vc.language)
  • Re: Named shared memory without synchronization
    ... Since this is an int, my assumption was that this scenario would not benefit ... As you already figured out `volatile' specifier is required in order to tell the compiler that it should not assume any optimizations regarding a variable. ... Alignment is important because unaligned variable may require more than one instruction to access it, therefore breaching atimicity of an access. ... can ever be validated with testing when dealing with synchronization issues. ...
    (microsoft.public.vc.language)