Questions tagged [happens-before]
Happens-before relates to multi-threading applications where logic has happened before a sequence of thread logic has occurred.
90
questions
1
vote
2answers
73 views
Unsafe publication in Java
In the book Java Concurrency in Practice by Brian Goetz et al.:
If you do not ensure that publishing the shared reference happens-before another thread loads that shared reference, then the write of ...
3
votes
1answer
79 views
On-the-fly comparator that works correctly with ConcurrentSkipListSet
I'm trying to use
queue = new ConcurrentSkipListSet<Task>(Comparators.comparing(Task::priority))
as a concurrent priority queue with unique elements (see a similar discussion here), but I need ...
0
votes
1answer
38 views
Will JVM update value of field for all threads with usage of Happens-Before, without assignment value to field directly?
I already know that if I write to non volatile field from another thread, he could probably cache it so all other threads won't see actual value. But if I would invoke for example start() on thread ...
3
votes
1answer
97 views
Java happens-before relationship invokeAndWait
My question is related to this question, which already has an answer:
yes, there is a happens-before relationship imposed between actions
of the thread calling invokeLater/invokeAndWait and actions ...
1
vote
2answers
155 views
HashMap vs ConcurrentHashMap: transfer between threads
I have a question about using maps in multithreaded application. Suppose we have such scenario:
Thread receives json data as List<Map<String, Object>> which is deserialized by Jackson ...
-1
votes
2answers
65 views
Why is 'happens-before; relationship called like that?
I understand everything in this concept, except why is it called like that. Can someone please help me understand it? It still makes me confused.. That is my only question.
I read several articles, I ...
0
votes
2answers
84 views
The Java volatile Read Visibility Guarantee
I came across the following excerpt while reading on visibility guarantees provided by the JVM when reading volatile variables :
"When thread A writes to a volatile variable and subsequently ...
2
votes
2answers
57 views
Volatile Java reordering
Firstly let me say that I am aware of this being a fairly common topic here but searching for it I couldn't quite find another question that clarifies the following situation. I am very sorry if this ...
1
vote
2answers
50 views
Can we treat final reorder rule in Java as happens-before rule?
There exist two final record rules in Java, as follows:
a write to final field in constructor and the constructed instance reference is assigned to variable afterwards can not be reordered
read a ...
1
vote
1answer
84 views
Volatile happens-before relationship when there's mix of volatile and non-volatile fields
I am trying to understand the happens-before behavior of the volatile field when there's a mix of volatile and non-volatile fields.
Let's say there's 1 WriteThread and 5 ReadThreads, and they update/...
1
vote
1answer
63 views
Java happens-before relationship?
Consider the following code.
public class Test {
private boolean running = false;
public void run() {
running = true;
}
public void test() {
boolean running1 = ...
0
votes
2answers
92 views
Does an atomic acquire synchronize with mutex lock release?
I have an object that stores some settings in an unordered_map with string keys and variant values. As my library might be used from several threads and reads will very likely outnumber writes by a ...
1
vote
1answer
67 views
wait() and notify() JMM semantics [closed]
I have a very particular question I couldn't find an answer to.
As we know, on entrance in synchronized blocks a thread performs a re-read of all the shared (non-local) variables in it's scope. ...
2
votes
1answer
48 views
java happens-before implementation in x86 architecture, t1 writes to main memory? [duplicate]
As expected the reader thread in below program runs forever since it cached the stop flag(non-volatile) on its local processor's cache. Whereas, as soon as I uncomment the println on reader thread, ...
1
vote
1answer
47 views
Do Android ART and HotSpot behave differently in non-volatile variable visibility?
I tested the below piece of code on HotSpot and Android ART, but with different results.
On HotSpot, MyThread never gets the updated isRunning, it get isRunning = true always...
But when I test it on ...
1
vote
1answer
35 views
Thread-safe in Singleton pattern which holder members
I have this sample code with Singleton pattern:
class Singleton{
private static Singleton instance;
private int count;
private Singleton(){}
public static synchronized Singleton getInstance(...
1
vote
0answers
47 views
How can I get a lock without happens-before?
I wanna a lock in java without happens-before to test other rules of memory visibility. And I've tried Unsafe#compareAndswapInteger, AtomicInteger#weakCompareAndSet, they seem still guarantee the ...
1
vote
2answers
96 views
Can I replace the atomic with a volatile in an one-reader/one-writer queue?
Let us consider the following one-reader/one-writer queue implemented with a linked list.
struct queue {
queue() {
tail = head = &reserved;
n = 0;
}
void push(item *it) {
tail-&...
2
votes
2answers
128 views
Ending the lifetime of an object used in another thread
What can the Standard say about the code below?
#include <iostream>
#include <chrono>
#include <thread>
int main()
{
static_assert(sizeof(int) == sizeof(float));
using ...
4
votes
1answer
168 views
What does “synchronization actions are totally ordered” mean?
I am reading Java Concurrency in Practice, in "16.1.3 The Java Memory Model in 500 words or less", it says:
The Java Memory Model is specified in terms of actions, which include reads and writes to ...
12
votes
1answer
357 views
What does “strongly happens before” mean?
The phrase "strongly happens before" is used several times in the C++ draft standard.
For example: Termination [basic.start.term]/5
If the completion of the initialization of an object with ...
12
votes
2answers
557 views
Kotlin coroutines “happens-before” guarantees?
Do Kotlin coroutines provide any "happens-before" guarantees?
For example, is there "happens-before" guarantee between write to mutableVar and subsequent read on (potentially) other thread in this ...
3
votes
1answer
60 views
Relationship between happens-before rules and initialization safety rule
I'm reading the Java Concurrency in Practice book.
When reading the chapter about JMM, it says:
The JMM defines a partial ordering called happens-before on all actions within the program. To ...
0
votes
1answer
177 views
Why is this Java program containing two volatile writes data race free?
Consider the following Java program:
static volatile int shared;
public static void main(final String[] args) {
final Runnable r = () -> {
shared = 1;
};
new Thread(r).start();
new ...
-1
votes
1answer
143 views
May the go compiler reorder following code?
Lately, I found some code looks like this:
var m map[int]int
func writem() {
tmpm := make(map[int]int)
for i := 0; i < 4000000; i++ {
tmpm[i] = i + 10
}
m = tmpm
}
func ...
5
votes
3answers
162 views
Does modification order contribute to happens-before relationship?
// Thread 1
// do A
x.store(1, std::memory_order_release); // operation1
// Thread 2
// do B
x.store(2, std::memory_order_release); // operation2
// Thread 3
x.load(std::memory_order_acquire); // ...
0
votes
1answer
73 views
Java memory consistency for Executor's worker threads
According to javadoc, an implementation of Executor must comply with:
Memory consistency effects: Actions in a thread (A) prior to submitting a Runnable object to an Executor happen-before its ...
1
vote
0answers
44 views
Java Memory Model applicable to actions and not statements
I got this from blog of A. Shipilev that JMM is applicable to actions and not statements with undermentioned code and statement
…they say that g = 1 happens-before int lg = g. This train-wrecks ...
-1
votes
2answers
44 views
happens-before relationship does not work as expected using “volatile”
int x,y;
volatile int z;
public static void main(String[] args) {
ThreadPoolExecutor pool = (ThreadPoolExecutor)Executors.newFixedThreadPool(5);
MainTest mission = new MainTest();
pool....
2
votes
2answers
173 views
Will a variable be read “fresh from main memory” if used in a loop AROUND a synchronized lock?
Please see the code below:
private static boolean flag=true; // main thread will call flag=false
private final static Object lock=new Object(); // lock condition
public static void thread1(){
...
2
votes
0answers
64 views
Java double check lock singleton must use the volatile keyword? [duplicate]
hi,i have a problem on double check lock in singleton pattern。 in follow code :
private volatile static DoubleCheckSingleton instance;
private DoubleCheckSingleton() {}
public static ...
6
votes
2answers
658 views
Why 'acquire/release' can not guarantee sequential consistency in c++11?
-Thread 1-
y.store (20, memory_order_release);
x.store (10, memory_order_release);
-Thread 2-
if (x.load(memory_order_acquire) == 10) {
assert (y.load(memory_order_acquire) == 20);
...
2
votes
1answer
78 views
Volatile write = volatile read
I have seen something like fld = fld a few times before but in all these cases the dummy write can be eliminated with a better performance.
public class Tst{
public volatile int fld = 1;
...
1
vote
2answers
83 views
Do I have the correct understanding of Can Reorder rules from the JSR-133 Cookbook?
In this link http://gee.cs.oswego.edu/dl/jmm/cookbook.html , there is this table that speaks about what can be reordered and cannot.
Here is the image
Now take a look at this quote from http://...
6
votes
5answers
822 views
Happens-before rules in Java Memory Model
I am currently studying for a concurrent programming exam and don't understand why the output of this program is 43. Why is x = y + 1 executed before t.start()? I also should explain which happens-...
1
vote
1answer
295 views
Is everything that happened before thread.start visible to the thread that is spawned calling start?
Now there are great answers to this already on stackoverflow but they don't give me the definite answer I want.
Say you have a method
Dosomething();
doAnother();
int x = 5;
Runnable r = new ...
11
votes
2answers
356 views
Happens-before for direct ByteBuffer
I have a direct ByteBuffer (off-heap) in one thread and safely publish it to a different thread using one of the mechanisms given to me by JMM. Does the happens-before relationship extend to the ...
6
votes
2answers
961 views
How to understand the channel communication rules in golang memory model?
Learning golang on the way, I got a little confused when trying to understand the channel communications described in the memory model spec as below:
A send on a channel happens before the ...
5
votes
2answers
199 views
How to establish a happens-before relationship between a request handling thread and a SocketChannel selector thread?
Consider a request-response protocol.
We spawn a thread to perform a select() loop for reads and writes on an accepted non-blocking SocketChannel. That might look something like
while (!isStopped()) ...
10
votes
2answers
331 views
Do I have reordering issue and is it due to reference escape?
I have this class where I cache instances and clone them (Data is mutable) when I use them.
I wonder if I can face a reordering issue with this.
I've had a look at this answer and JLS but I am still ...
3
votes
1answer
123 views
Java multithreading without happens-before relationship
I have two threads using the same Object obj and as I have read every change made in obj by Thread A will be visible for Thread B due to happens-before relationship.
What I want to do is the opposite,...
0
votes
0answers
30 views
Are operations before Thread.start() thread safe in relation to the new thread? [duplicate]
Are there any guarantees to the variable visibility and/or ordering of instructions before Thread.start() in relation to the starting of the new thread?
Specifically, in the minimalistic example ...
0
votes
3answers
193 views
Happens-before with different monitors
Somewhere on this site (I don't remember the question) someone claimed that happens-before relashionship holds for different monitors and for different volatile variables, i.e.
// Thread T1
...
9
votes
1answer
855 views
How deep volatile publication guarantees?
As is known guarant that if we have some object reference and this reference has final field - we will see all reachable fields from final field(at least when constructor was finished)
example 1:
...
7
votes
1answer
107 views
Does this non-standard Java synchronization pattern work?
Let's say I have two threads running like this:
Thread A which performs computation while updating pixels of a shared image
Thread B periodically reads the image and copies it to the screen
Thread A ...
0
votes
1answer
454 views
Does Consul key-value store give us any guarantees on ordering of two writes with two different keys?
Suppose we have only two nodes A and B.
When we issue these two commands on the node A,
$ curl -X PUT -d one .../v1/kv/key1
$ curl -X PUT -d two .../v1/kv/key2
(ie. PUT one key1 happens-before PUT ...
0
votes
1answer
232 views
java concurrency - Is Instruction Level Parallelism(ILP) used underhood
Concurrency in Java or some similar languages is achieved through threads or task level parallelism. But under the hood does the hardware or run time also use ILP to achieve best performance.
Little ...
-2
votes
1answer
389 views
is happens before i.e hb(x,y) not same as hb(y,x)?
Acceptable answers after looking at answers, comments and jls-17.4.2
1) anti-symmetric property holds like pointed by - user3707125
2) the hb relationship between locks is actually between ...
3
votes
2answers
145 views
JMM In Practice
Consider the following multithreaded code written in Java:
Shared variables:
boolean n; // non-volatile
volatile boolean v; // volatile
Thread 1:
v = true;
System.out.println("n=" + n)...
5
votes
2answers
474 views
if thread A start before thread B in java,then A will be scheduled by os before B?
I am reading 《Understanding the JVM Advanced Features and Best practices》 that has a code segment that explains happens-before rule in java. I cannot understand. The code is below:
private int value =...