Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Fun with multi-threading
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: