Re: volatile




"Timo Stamm" <timo.stamm@xxxxxxxx> wrote in message
news:442d5ab4$0$7760$9b4e6d93@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Timo Nentwig schrieb:
Timo Stamm wrote:
| The volatile modifier requests the Java VM to always access the shared
| copy of the variable so the its most current value is always read. If
| two or more threads access a member variable, AND one or more threads
| might change that variable's value, AND ALL of the threads do not use
| synchronization (methods or blocks) to read and/or write the value,
| then that member variable must be declared volatile to ensure all
| threads see the changed value.

Yes...

Okay, some shorter pseudo-code:

thread1{
int value;

void modify(){ value++; }
void print() {System.out.println( value ); }
}

thread2{
thread1.modify();

// IMHO I don't need volatile here:
thread1.print();

// but I wouldneed it here:
System.out.println(thread1.value);
}

I don't think you need "volatile" in this case. There are no /concurrent/
modifications: thread2 does the modification (through thread1.modify), and
reads the value /afterwards/.


Without either volatile or synchronization, there's no guarantee thread 1
will see a change made in thread 2; thread 1 might hold onto its previously
cached value indefinitely.

By the way

value++

should be a red flag. If two threads can both execute it, volatile isn't
good enough, becsue you have no guarantee about how

fetch value
increment
store the result in value

get interleaved. Nothing less than

synchronize(lock)
{
value++;
}

is reliable.


.



Relevant Pages

  • 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: Two more multithreading questions
    ... 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. ... is that when one thread executes assignment statements to volatile ... It is possible that one thread starts executing an assignment statement ... JLS 17.4.4 Synchronization Order ...
    (comp.lang.java.programmer)
  • 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)