3 ms·
Here's a working race condition with async: https://gist.github.com/39409838626b22433f8bf1878276b3c1 https://gist.github.com/39409838626b22433f8bf1878276b3c1
by lamontcg 5y ago
Here's a working race condition with async:
https://gist.github.com/39409838626b22433f8bf1878276b3c1 https://gist.github.com/39409838626b22433f8bf1878276b3c1
Prints 1 instead of 1000
If you remove the sleep there's no race because the fiber scheduler never hits a blocking call anywhere, but the sleep stands in for some kind of i/o. And then there's the terrible global variable.
(Conversely this does show you how badly you have to write code to get race conditions with async and the sleep there is important to the generation of the race, which would not be necessary with threads)
And I think if you remove the sleep it doesn't race due to the GIL and single-threadedness of ruby for pure userspace computations?
- ricardobeat 5y agoIf it did not print 1 then you'd have a problem. Not really a race condition, just the semantics of async. You'll get the same with goroutines, javascript async/await, and other constructs.