-4

Say you have:

   volatile boolean called  = false;  

Is this

   if ( !called  && (called = true) ) { ... do once ... } 

an atomic operation?

I know about AtomicBoolean. The question is not about that so try to resist your urges.

mjs
  • 21,431
  • 31
  • 118
  • 200
  • 4
    No, it's not. How could it be? You clearly have two parts here: 1 reading the current value of called and testing if it's false; 2. assigning true to it. – JB Nizet Aug 17 '17 at 10:14
  • 2
    Short answer: "No, that's not an atomic operation". – Erwin Bolwidt Aug 17 '17 at 10:15
  • I'm tempted to vote to close as duplicate to https://stackoverflow.com/questions/4876122/when-is-it-preferable-to-use-volatile-boolean-in-java-rather-than-atomicboolean?rq=1 – Mark Rotteveel Aug 17 '17 at 10:22
  • @MarkRotteveel It's a different question. – mjs Aug 17 '17 at 19:03
  • @ErwinBolwidt The question should maybe have been phrased differently. A better phrasing might have been to say, is it synchronizably safe ( the contents of the if block ) which i'd argue it is since I find it hard another thread could get in between while that same one line command is still being executed. Note, by saying finding it hard, I really mean impossible as such statements from experience from java's concurrency package it appears similar one liners are used quite often as a guarantee to avoid race conditions. Are there any situations where two threads can execute if? – mjs Aug 17 '17 at 19:05
  • @momomo I said "tempted" and actually some of those answers address a similar example. – Mark Rotteveel Aug 17 '17 at 20:23
  • @momomo But it isn't a 'one line command'. See my answer. Posted nine hours before your comment. – user207421 Aug 17 '17 at 22:57

1 Answers1

2

In a naive implementation this could boil down to:

    LOAD A    ; called
    NEG       ; negate
    BRF 1$    ; branch if false
    LOAD 1    ; true
    STOR A    ; store into called
1$:           ; do once

Not exactly atomic, is it? The Java compiler probably changes the NEG/BRF sequence to BRT (branch if true), but that's about as much help as you can rationally expect.

Strange question.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Well, i guess a better phrasing might have been to say, is it synchronizably safe ( the contents of the ifblock ) which i'd argue it is since I find it hard another thread could get in between while that same one line command is still being executed. Note, by saying finding it hard, I really mean impossible as such statements from experience from java's concurrency package it appears similar one liners are used quite often as a guarantee to avoid race conditions. – mjs Aug 17 '17 at 19:03
  • 1
    @momomo feelings and guesses are not how you write code that behaves correctly under concurrent access. And if you think you have seen code like this in Java, then provide links. Context is important. – Mark Rotteveel Aug 17 '17 at 20:22
  • I am not guessing, I am trying to find out. Are there any circumstances where the if block could execute twice? I am curious more than actually using it. – mjs Aug 17 '17 at 21:04
  • 2
    @momomo You baffle me. I have clearly demonstrated that it isn't a 'one line command' but four or five byte-code instructions, with three or four points between them where another thread could be interleaved. There is nothing remotely threadsafe about this construction. What you're looking for is a test-and-set operation such as provided by `AtomicBoolean.compareAndSet()`. – user207421 Aug 17 '17 at 22:52