Lines of ruby ​​flow?

I am reading about Ruby Threads (I have never used Ruby before) and I am surprised by the following code:

t1 = Thread.new { print "w"; Thread.pass; print "a"}
t2 = Thread.new { print "e"; Thread.pass; print "l"}
t1.join
t2.join

The book said that it would always show "weakness", and I do not understand why, because if they are real threads (as in other languages), it is not guaranteed that "w" will print before "e".

Thank you for your responses!

+4
source share
1 answer

Modify the book or read it with care. You are right, of course, the only guarantee is what awill be displayed after w, etc.

I just checked your code and here are the results after 10 starts:

weal
weal
ewal
wela
wael
wael
wela
ewla
ewla
weal
+8
source

Source: https://habr.com/ru/post/1542500/


All Articles