0

Single JVM instruction is not atomic but single assemble instruction is atomic. Is this correct?

Demo

public class DemoInstruction {
    public static void main(String[] args) {
        int i=0;
        i++;
    }
}

i++ translate into JVM instruction:

    IINC 1 1

i++ translates into local assembly instructions consisting of three assembly instructions.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Machi
  • 403
  • 2
  • 14
  • I believe this is a duplicate of https://stackoverflow.com/q/23232242 but I'm not sure, as I'm having difficulty understanding your English. – Dawood ibn Kareem Feb 04 '23 at 05:46
  • 2
    *but single assembly instruction is atomic* - no, not true. Even CISC machines like x86 that can run a memory-destination add or increment as a single instruction like `inc dword ptr [rdi]` don't do it atomically unless you use a special prefix in the machine code to make that happen. [Can num++ be atomic for 'int num'?](https://stackoverflow.com/a/39396999). `lock inc dword ptr [rdi]` is atomic, and what C++ compilers use for `std::atomic`. Or what JVMs use for AtomicInteger. – Peter Cordes Feb 04 '23 at 06:09
  • @Sweeper: you might want to add [Can num++ be atomic for 'int num'?](https://stackoverflow.com/a/39396999) to the duplicate list; it's a canonical Q&A about atomicity in assembly, including the fact that a single asm instruction is only atomic on a single-core machine. I can't add it myself since unfortunately I'm only about half way to an [atomic] tag badge, and this isn't tagged [assembly] despite the question being about that. – Peter Cordes Feb 04 '23 at 06:13

0 Answers0