On Wed, Dec 21, 2011 at 12:00 AM, Tony Arcieri <tony.arcieri / gmail.com> wrote:
>
> This pattern should work, although it's a bit ugly:
>
> t1=Thread.new{work(url1)}
> t2=Thread.new{work(url2)}
> t3=Thread.new{work(url3)}
> t1.join
> t2.join
> t3.join
>
>
> A better approach would be:
>
> threads = [url1, url2, url3].map { |url| Thread.new { work(url) } }

This is unsafe, because you reuse "url": because of the scoping rules
a thread might see another thread's value.  Better do this:

threads = [url1, url2, url3].map { |url| Thread.new(url) {|u| work(u) } }

> # joins each thread and obtains the value returned from the block you
> passed to Thread.new
> results = threads.map(&:value)

Yep, Thread#value is vastly useful although rarely used.

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/