Questions tagged [memory-visibility]
35 questions
210
votes
5 answers
Why does this Java program terminate despite that apparently it shouldn't (and didn't)?
A sensitive operation in my lab today went completely wrong. An actuator on an electron microscope went over its boundary, and after a chain of events I lost $12 million of equipment. I've narrowed down over 40K lines in the faulty module to…

Dog
- 7,707
- 8
- 40
- 74
100
votes
7 answers
Are static variables shared between threads?
My teacher in an upper level Java class on threading said something that I wasn't sure of.
He stated that the following code would not necessarily update the ready variable. According to him, the two threads don't necessarily share the static…

dontocsata
- 3,021
- 3
- 20
- 19
15
votes
2 answers
Volatile variables and other variables
The following is from classical Concurency in Practice:
When thread A writes to a volatile variable and subsequently thread B
reads the same variable, the values of all variables that were
visible to A prior to writing to the volatile…

Cratylus
- 52,998
- 69
- 209
- 339
14
votes
4 answers
in what architectures/OS other thread can see default nonfinal field values after constructor call?
I'm trying to reproduce a memory visibility issue in case of insufficient object initialization for non-final fields (JLS 17.5 Final Field Semantics, FinalFieldExample class example). Where it stated "However, f.y is not final; the reader() method…

yetanothercoder
- 1,689
- 4
- 21
- 43
14
votes
3 answers
Java ConcurrentHashMap.computeIfPresent value modification visibility
Let's say I have a concurrent map with collections as value:
Map map = new ConcurrentHashMap<>();
map.putIfAbsent(8, new ArrayList<>());
and I update the value as follows:
map.computeIfPresent(8, (i, c) -> {
c.add(5);
…

Marin Veršić
- 404
- 4
- 10
12
votes
5 answers
Is unsynchronized read of integer threadsafe in java?
I see this code quite frequently in some OSS unit tests, but is it thread safe ? Is the while loop guaranteed to see the correct value of invoc ?
If no; nerd points to whoever also knows which CPU architecture this may fail on.
private int invoc =…

krosenvold
- 75,535
- 32
- 152
- 208
12
votes
2 answers
Does atomic variables guarantee memory visibility?
Small question about memory visibility.
CodeSample1:
class CustomLock {
private boolean locked = false;
public boolean lock() {
if(!locked) {
locked = true;
return true;
}
return false;
…

JamesJenkins
- 131
- 4
6
votes
3 answers
Visibility Guarantee
I have read a few explanations of section 16.3 "Initialization Safety" from JCIP and am still not clear. The section states that
"Further, any variables that can be reached through a final field of a properly constructed object (such as the elements…

CaptainHastings
- 1,557
- 1
- 15
- 32
5
votes
4 answers
jvm reordering/visibility effect test
While writing some java article I'm trying to reproduce re-ordering in case of unsynchronized object costruction in multithreaded environment.
The case when a heavy object is constructed w/o synchonization/volatiles/finals and other threads get…

yetanothercoder
- 1,689
- 4
- 21
- 43
5
votes
3 answers
Atomic operation propagation/visibility (atomic load vs atomic RMW load)
Context
I am writing a thread-safe protothread/coroutine library in C++, and I am using atomics to make task switching lock-free. I want it to be as performant as possible. I have a general understanding of atomics and lock-free programming, but I…

RmbRT
- 460
- 2
- 10
4
votes
0 answers
Visibility guarantee with CompletableFuture's thenAccept
I'm trying to understand if there are any visibility-guarantees provided by CompletableFuture.
Suppose I've a class called SampleClass which is something like the following:
public class SampleClass {
private String member1;
private String…

Lavish Kothari
- 2,211
- 21
- 29
4
votes
2 answers
C++11 atomics. visibility and thread.join() / correct way to stop a thread
For which (if any?) STORE_ORDER & LOAD_ORDER does C++11 guarantee that this code runs in finite time?
std::atomic a{false};
std::thread t{[&]{
while(!a.load(LOAD_ORDER));
}};
a.store(true, STORE_ORDER);
t.join();
I see two issues with…

Chronial
- 66,706
- 14
- 93
- 99
4
votes
0 answers
How to demonstrate memory visibility problems in Go?
I'm giving a presentation about the Go Memory Model. The memory model says that without a happens-before relationship between a write in one goroutine, and a read in another goroutine, there is no guarantee that the reader will observe the…

stephenbez
- 5,598
- 3
- 26
- 31
4
votes
1 answer
If Thread B wishes to see changes Thread A makes, can only the last change be to a volatile variable as opposed to all?
I've looked at this answer, and it states how:
Under the new memory model, when thread A writes to a volatile
variable V, and thread B reads from V, any variable values that were
visible to A at the time that V was written are guaranteed now to…

Mario Ishac
- 5,060
- 3
- 21
- 52
4
votes
3 answers
Is volatile needed?
if I have a byte queue, where it is expected to have one thread producer, another consumer:
class ByteQueue{
byte[] buf;
/*volatile?*/ int readIdx;
/*volatile?*/ int writeIdx;
Runnable writeListener;
Runnable readListener;
//…

fbenoit
- 3,220
- 6
- 21
- 32