From: Paul Brannan Date: 2008-11-11T22:59:27+09:00 Subject: [ruby-core:19821] Re: use of require thread safety On Tue, Nov 11, 2008 at 10:51:45AM +0900, Nobuyoshi Nakada wrote: > Hi, > > At Sat, 8 Nov 2008 09:55:59 +0900, > Roger Pack wrote in [ruby-core:19731]: > > I'm sure this has been discussed before, but...should there be > > anything to help in the case that two threads require the same file at > > the same time [i.e. the second thread requires a file while a first is > > still loading it and thus continues assuming the file was successfully > > loaded]? > > While a thread is requiring a given file, another thread which > requires the same file will be blocked until the former thread > finishes loading, and the result will be false. As Charlie pointed out, this behavior can cause a deadlock: - Thread 1 requires a.rb - Thread 2 requires b.rb - Thread 2 requires a.rb and blocks - Thread 1 requires b.rb and blocks [pbrannan@zem tmp]$ cat test.rb t1 = Thread.new { require 'a' } t2 = Thread.new { require 'b' } t1.join t2.join [pbrannan@zem tmp]$ cat a.rb puts "#{Thread.current}: a.rb" sleep 1 puts "#{Thread.current}: requiring b" require 'b' [pbrannan@zem tmp]$ cat b.rb puts "#{Thread.current}: b.rb" puts "#{Thread.current}: requiring a" require 'a' [pbrannan@zem tmp]$ ruby1.9 test.rb #: a.rb #: b.rb #: requiring b #: requiring a test.rb:3:in `join': Interrupt from test.rb:3:in `
' Paul