Aug 26, 2017 · 1 min read
This is an example of a race-condition :)
It is due to the fact that multiple threads read and update the same @executed variable. E.g. 2 threads read the variable concurrently before the variable was updated:
time | thread1 | thread2 | thread3
↓ | @executed == false | |
↓ | | @executed == false |
↓ | puts "executing!" | |
↓ | @executed = true | |
↓ | | | @executed == true
↓ | | puts "executing!" |
↓ | | @executed = true |If you run the code multiple times, you’ll see that it prints executing! sometimes 3 or even more times.
