Today, I encountered my first Ruby gripe.
Variables mapped from collection passed into Ruby threads are not independent of one another. If one thread changes the passed variable, all of other threads share the new variable.
Let’s suppose I have an Array of items = ["item 1", "item 2", "item 3"]
Here’s some code that takes each item in the array and does some processing on it, printing out “finished!” statements after each element is processed.
items.map{|item| Thread.new(item){ #process with variable execution time goes here puts "#{item} finished!" }}
Let’s suppose the map task associated with “item 3″ starts last and ends first. Then the output could look like this:
item 3 finished!item 3 finished!item 3 finished!
The explanation for this peculiar behavior is detailed in Ruby Central‘s Pragmatic Programmer’s Guide:
“A thread shares all global, instance, and local variables that are in existence at the time the thread starts.”
clara tryin 9:01 pm on June 7, 2009 Permalink
hey thanks for the info,on how google is soooooooooooooooooooo awsome hahahhaha!!!!!!!!!!!!!!!!!!!!!! thanks kevin chui……….are you chinese???????? cause your name sounds like it………….not to be races or anything hahhahahahahhahaahahahahh
clara tryin 9:04 pm on June 7, 2009 Permalink
when will you write/email me back??????????????????????hahahahaahahahahahahahhahahahahahahahahahahahahahahahahahahahahahahaahahahahahahahhaahhahahahaahahhaahahhaahhahahahaahahhaahahahahhahaahhhahhahahahaahahahhaahahahahah this can go on forever……..even until you write/email me back hahahahhahahahahahahahahahahahahahahahahahhahahahahahahahahahahahahahahaahhaahhaahahhahaahahhahahahahaahhahahahahahahahhaahhahahahahahehe
clara tryin 9:08 pm on June 7, 2009 Permalink
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::)))))))))))))))))))))))))))))))))))))))))))))))))))))))
======================================]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::((((((((((((((((((((((((((((((((((((((((((((((((((((((
=======================================[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
clara tryin 9:10 pm on June 7, 2009 Permalink
those are sad and happy faces hahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahhahahhahaahhahhahahhhahahahahahahahahahahahahahahahahahahahahahhahahahaha
clara tryin 9:11 pm on June 7, 2009 Permalink
it seems like i am the inly one who leaves comments hhhhhhhhhhhhhaaaaaaaaaaaaaa
clara tryin 9:14 pm on June 7, 2009 Permalink
i am a lucky person being able to write on the computer hahahahahahahha………………………yes yes yes i know i know i laugh a lot hahahahahaah oh uppes i did it a gain hhahahahahaahah i guess i am quit the laughter hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhaaaaaaaaaaaaaaaaaaaaahhhhhhhhhaaaaaaaaaaaaahhahahahahahahaahahaah i just love to laugh hahahahahahahahahahahhahahahahahahahahahahahahahahahahahahahahahahaha
Kevin Chiu 9:10 am on June 8, 2009 Permalink
Hello? What are you looking for?