Hello,
I try to use File.flock to lock a file in a cgi script, but I can't get
it to work in my test script.
It creates a file and write "foo". A thread that will write "bar" in 0.1
secondes is started, and an infinite loop reads the file and print the
result.
The script displays some "foo" and waits, probably for the file to be
unlocked. No "bar". I expected some "foo", then many "bar".
Here is the script:
filename = "/tmp/flock-test"
f = open(filename, "w")
f.puts "foo"
f.close
Thread.new {
sleep .1
f = open(filename, "w")
f.flock(File::LOCK_EX)
f.puts "bar"
f.flock(File::LOCK_UN)
f.close
}
loop do
f = open(filename)
f.flock(File::LOCK_EX)
puts f.gets
f.flock(File::LOCK_UN)
f.close
end
With the help of some puts, it seems to be blocking on f.gets in the
main thread and f.flock in the writing thread.
If I add "sleep .0001" in the main loop, it works.
Where am I wrong?
I tried to search "flock" on ruby-talk.com, but I got an Internal Server
Error.
Mike.
midulo.