-3

I want to do something like this

while (true) {
    if (my_boolean) {  //my_boolean is being updated by other threads
        do something;
        break;
    }
    do something else;
} 

my problem is, my_boolean is being updated by another method in another thread. Sometimes such updates can be captured, sometimes not. Is there any consistent way to work around this?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

1 Answers1

1

You can declare that boolean as volatile.

Dorian Gray
  • 2,913
  • 1
  • 9
  • 25