Forum: Ruby thread local variables

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9528ea68a2efbf9093b1d41f7f269f36?d=identicon&s=25 Russell Fulton (Guest)
on 2006-01-26 21:27
Hi Folk,
         I have a program that creates threads in a loop like so:

file.each{|line|
  ti = getThreadIndex()
  threads[ti] = thread(line) { |line|

    do_stuff(line)
  }


}

It appears from my observation that the value passed to do_stuff is
*not* local to the thread. Instead it is what ever the current line is
in the outer loop.  If I change { |line| to {linex then it works
properly.

getThreadInded selects an unused thread from the array.

Is this as intended? If so I find it very confusing!

BTW I'm running ruby 1.8.2 (2004-12-25) [powerpc-darwin8.0]
 (as shipped by Apple)

Russell
This topic is locked and can not be replied to.