Sha256: bed378101364f79ee1a41ba583854b2d1049041e448452f9e8436ae25aed4b90

Contents?: true

Size: 482 Bytes

Versions: 1

Compression:

Stored size: 482 Bytes

Contents

# This is a thread-safe version of ThreadUnsafeRunAtMostOnce. It wraps the call with a mutex which
# guarantees that the critical section is only ever run in one thread at a time, thereby making this
# safe to use and call from multiple threads.
class ThreadSafeRunAtMostOnce
  def initialize(&blk)
    @blk = blk
    @ran = false
    @mutex = Mutex.new
  end

  def call
    @mutex.synchronize do
      unless @ran
        @ran = true
        @blk.call
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
thread_weaver-0.1.0 examples/thread_safe_run_at_most_once.rb