Skip to content

Instantly share code, notes, and snippets.

@monzou
Created December 29, 2011 14:13
Show Gist options
  • Save monzou/1534261 to your computer and use it in GitHub Desktop.
Save monzou/1534261 to your computer and use it in GitHub Desktop.
Ruby でファイルをモニタリングするとこんな感じなのかなぁ?
require 'fssm'
require 'thread'
PATH = "/Users/monzou/sample.txt"
class Monitor
def start(&block)
puts "start monitoring @ #{Thread.current}"
monitor = FSSM::Monitor.new
monitor.path(File.dirname(PATH), File.basename(PATH)) do
update(&block)
end
thread = Thread.new { monitor.run }
thread.join
end
end
class Model
attr_accessor :value
def initialize
@value = ""
end
end
puts "boot @ #{Thread.current}"
model = Model.new
monitor = Monitor.new
mutex = Mutex.new
monitor.start do
puts "update @ #{Thread.current}"
# is it thread-safe ?
mutex.synchronize do
model.value << "+"
puts model.value
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment