4 ms·
They are defined behavior in Java, but not in C++. Things still get extremely complicated[1] if you use them in Java, so I would treat them as undefined behavio
by kbwt 9y ago
They are defined behavior in Java, but not in C++. Things still get extremely complicated[1] if you use them in Java, so I would treat them as undefined behavior regardless and ban them from any Java code base.
Note that Java has some implicit synchronization built-in, this is to preserve memory safety in incorrectly synchronized code. That comes at the cost of some overhead which will only increase as systems get built with more cores.
[1] Read the three sections starting from https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.4.6 https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.htm... for the details. Suffice to say you have to consider all possible interleavings of memory accesses, not unlike trying to reason about a non-deterministic Turing machine. In particular, the causality requirements leave some room for unexpected executions, as they do not apply equally to all inter-thread actions. This can REALLY bite you in the ass if you only test on particular VM implementations or architectures.
- hsivonen 9y agoPeople who, like me, were taught concurrency at a time when Java had a proper memory model but C and C++ did not (i.e. prior to 2011) should go read the docs for C11 memory orders (used also by Rust and C++). Writing lockless Rust/C/C++ leads to a bad time (especially on non-x86 architectures) if one believes what's true about concurrency in Java to be generally true on computers.
- pjmlp 9y agoThere is a guy in the C++ community known for writing lock-less data structures (Tony Van Eerd), if I recall correctly one of his advices is to think multiple times before ever considering writing such kind of code.
- gpderetta 9y agoTechnically C11 uses the C++11 memory model. It was originally developed as part of the C++ standardization effort.