Posted: Sat Mar 27, 2004 10:53 am Post subject: Fun with multi-threading
I've been playing with threads in Ruby (though you can do multi-threading in lots of languages, it's really easy in Ruby), and just found something kind of interesting.
code:
>> ["hello", "world", "super calla fragalistic"].each do |x|
*> Thread.new do
*> puts x
>> end
>> end
helloworld
super calla fragalistic
=> ["hello", "world", "super calla fragalistic"]
>> ["hello", "world", "super calla fragalistic"].each do |x|
*> puts x
>> end
hello
world
super calla fragalistic
=> [1, 2, 3]
>> exit
C:\>
It turns out that the puts method is so fast, that running it in two separate threads the second wrote to standard output before the first even had a chance to add a newline. puts adds a newline if the thing being printed doesn't already end in one.