Questions tagged [happens-before]

Happens-before relates to multi-threading applications where logic has happened before a sequence of thread logic has occurred.

108 questions
42
votes
4 answers

Memory Consistency - happens-before relationship in Java

While reading Java docs on Memory Consistency errors. I find points related to two actions that creates happen - before relationship: When a statement invokes Thread.start(), every statement that has a happens-before relationship with that…
Prateek
  • 12,014
  • 12
  • 60
  • 81
39
votes
3 answers

Java memory model: volatile variables and happens-before

I'd like to clarify how happens-before relation works with volatile variables. Let we have the following variables: public static int i, iDst, vDst; public static volatile int v; and thread A: i = 1; v = 2; and thread B: vDst = v; iDst = i; Are…
17
votes
1 answer

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 static storage duration strongly happens before a call to…
curiousguy
  • 8,038
  • 2
  • 40
  • 58
16
votes
2 answers

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 case: suspend fun doSomething() { var mutableVar…
Vasiliy
  • 16,221
  • 11
  • 71
  • 127
15
votes
3 answers

Does Java allow a volatile read to be optimized away if the value isn't needed, also removing the happens-before synchronization?

The following code sample shows a common way to demonstrate concurrency issues caused by a missing happens-before relationship. private static /*volatile*/ boolean running = true; public static void main(String[] args) throws…
stonar96
  • 1,359
  • 2
  • 11
  • 39
12
votes
2 answers

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 native (off-heap) memory wrapped by the ByteBuffer? If…
Philippe Marschall
  • 4,452
  • 1
  • 34
  • 52
10
votes
2 answers

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 not confident. public class DataWrapper { …
Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76
10
votes
2 answers

Does .awaitTermination() establish happens-before with work done in the executor?

Question I've had for years: In this pseudocode, ExecutorService svc = Executors.newFixedThreadPool(3); svc.submit(new Runnable() { /* code A */ }); svc.shutdown(); if(svc.awaitTermination(...)) { // code B .awaitTermination() is not documented…
10
votes
4 answers

Java Happens-Before and Thread Safety

Suppose I have a class which wraps a HashMap as follows: public final class MyClass{ private final Map map; //Called by Thread1 public MyClass( int size ){ this.map = new HashMap( size ); …
8
votes
3 answers

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 corresponding receive from that channel completes. The…
Roy Ling
  • 1,592
  • 1
  • 14
  • 31
8
votes
1 answer

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: class Foo{ private final Map map; Foo(){ …
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
7
votes
1 answer

How are the final multi-threading guarantees and the memory model related in Java?

The memory model is defined in 17.4. Memory Model. The final field multi-threading guarantees are given in 17.5. final Field Semantics. I don't understand why these are separate sections. AFAIK both final and the memory model provide some…
poq
  • 71
  • 3
7
votes
5 answers

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-before rules I used. If I understand the program…
7
votes
1 answer

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 performs work quickly, say 1 million updates per…
7
votes
2 answers

Does a Java Lock object enforce a happens-before relationship?

Java provides a Lock object in the concurrency package that according to the documentation provides more extensive locking operations than can be obtained using synchronized methods and statements. The synchronized methods/blocks besides mutual…
Petrakeas
  • 1,521
  • 15
  • 28
1
2 3 4 5 6 7 8