Class: Mutex

Inherits:
Object show all
Defined in:
lib/qualitysmith_extensions/mutex/if_available.rb

Instance Method Summary collapse

Instance Method Details

#if_availableObject

Acts like synchronize, except that if the lock cannot be acquired immediately, the program continues without executing the given block.

Example:

mutex = Mutex.new
# ...
mutex.if_available do
  # Some process that we only want one thread to be running at a time,
  # and we don't mind skipping if some other thread is already doing it.
  loop do
    notify_mechanics if danger_to_teh_manifold!
    sleep 60
  end
end


25
26
27
28
29
30
31
32
33
# File 'lib/qualitysmith_extensions/mutex/if_available.rb', line 25

def if_available
  if try_lock
    begin
      yield
    ensure
      unlock
    end
  end
end