On 12/23/05, J. Ryan Sobol <ryansobol / gmail.com> wrote:> Correct me if I'm wrong, but your examples only prove that the thread> on the CPU will be able to append the file. I *think* Matias wants> to know if the statement ( File.new('filename','a').puts("this is the> string") ) is atomic. Or in other words, do you need to enforce> mutual exclusive access to the file with a mutex? Unfortunately, I> don't have an answer to that question.
[kig@jugend:~] cat fw_test.rbdef 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.rbputs in different threads seems to be atomic