exAspArk
exAspArk
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.

)

    exAspArk

    Written by

    exAspArk

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade