📄 12 - locking a file.rb
字号:
def flock(file, mode) success = file.flock(mode) if success begin yield file ensure file.flock(File::LOCK_UN) end end return successend#---open('output', 'w') do |f| flock(f, File::LOCK_EX) do |f| f << "Kiss me, I've got a write lock on a file!" endend#---def open_lock(filename, openmode="r", lockmode=nil) if openmode == 'r' || openmode == 'rb' lockmode ||= File::LOCK_SH else lockmode ||= File::LOCK_EX end value = nil open(filename, openmode) do |f| flock(f, lockmode) do begin value = yield f ensure f.flock(File::LOCK_UN) # Comment this line out on Windows. end end return value endend#---t1 = Thread.new do puts 'Thread 1 is requesting a lock.' open_lock('output', 'w') do |f| puts 'Thread 1 has acquired a lock.' f << "At last we're alone!" sleep(5) end puts 'Thread 1 has released its lock.'endt2 = Thread.new do puts 'Thread 2 is requesting a lock.' open_lock('output', 'r') do |f| puts 'Thread 2 has acquired a lock.' puts "File contents: #{f.read}" end puts 'Thread 2 has released its lock.'endt1.joint2.join# Thread 1 is requesting a lock.# Thread 1 has acquired a lock.# Thread 2 is requesting a lock.# Thread 1 has released its lock.# Thread 2 has acquired a lock.# File contents: At last we're alone!# Thread 2 has released its lock.#---def try_lock puts "I couldn't get a lock." unless open_lock('contested', 'w', File::LOCK_EX | File::LOCK_NB) do puts "I've got a lock!" true endendtry_lock# I've got a lock!open('contested', 'w').flock(File::LOCK_EX) # Get a lock, hold it forever.try_lock# I couldn't get a lock.#---
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -