From: "J. Ryan Sobol" Date: 2005-12-24T06:59:55+09:00 Subject: Re: multithreaded file access On Dec 23, 2005, at 4:13 PM, Ilmari Heikkinen wrote: > [kig@jugend:~] cat fw_test.rb > def writer(i, fn, ok) > Thread.new{ > t_str = "#{i}" * 65536 > while ok.first > File.open(fn, 'a'){|f| > f.puts t_str > } > end > } > end > > ok = [true] > fn = 'fw_test.dat' > ts = (1..3).map{|i| writer i, fn, ok} > > sleep 10 > > ok[0] = false > > if File.readlines(fn).uniq.size == ts.size > puts "puts in different threads seems to be atomic" > else > puts "puts in different threads isn't atomic" > end > > File.unlink fn > > [kig@jugend:~] ruby fw_test.rb > puts in different threads seems to be atomic Crafty test program. Coincidentally, my results differ from yours. $ ruby fw_test.rb puts in different threads isn't atomic $ ruby -v ruby 1.8.2 (2004-12-25) [powerpc-darwin8.3.0] I'm getting 8 unique lines in File.readlines(fn).uniq, as opposed to the 3 thread objects in ts. Assuming I understand the program, that means threads are not waiting their turn like they should. FYI, I've compiled my own ruby executable from DarwinPorts instead of using the 'broken' one shipped in OS X Tiger. However, if I change the multiplication factor in line 3 to 655, then $ ruby fw_test.rb puts in different threads seems to be atomic ~ ryan ~ PS - I love the 205+ MB of text this app generates! :-D