module MonitorMixin
In concurrent programming, a monitor is an object or module intended to be used safely by more than one thread. The defining characteristic of a monitor is that its methods are executed with mutual exclusion. That is, at each point in time, at most one thread may be executing any of its methods. This mutual exclusion greatly simplifies reasoning about the implementation of monitors compared to reasoning about parallel code that updates a data structure.
You can read more about the general principles on the Wikipedia page for Monitors.
Examples
Simple object.extend
require 'monitor.rb' buf = [] buf.extend(MonitorMixin) empty_cond = buf.new_cond # consumer Thread.start do loop do buf.synchronize do empty_cond.wait_while { buf.empty? } print buf.shift end end end # producer while line = ARGF.gets buf.synchronize do buf.push(line) empty_cond.signal end end
The consumer thread waits for the producer thread to push a line to buf while buf.empty?. The producer thread (main thread) reads a line from ARGF and pushes it into buf then calls empty_cond.signal to notify the consumer thread of new data.
Simple Class include
require 'monitor' class SynchronizedArray < Array include MonitorMixin def initialize(*args) super(*args) end alias :old_shift :shift alias :old_unshift :unshift def shift(n=1) self.synchronize do self.old_shift(n) end end def unshift(item) self.synchronize do self.old_unshift(item) end end # other methods ... end
SynchronizedArray implements an Array with synchronized access to items. This Class is implemented as subclass of Array which includes the MonitorMixin module.
Public Class Methods
(untyped obj) → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/monitor/0/monitor.rbs, line 201
def self.extend_object: (untyped obj) -> untyped
(*untyped) { (*untyped) → untyped } → void
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/monitor/0/monitor.rbs, line 283
def initialize: (*untyped) { (*untyped) -> untyped } -> void
Use extend MonitorMixin or include MonitorMixin instead of this constructor. Have look at the examples above to understand how to use this module.
Public Instance Methods
() → nil
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/monitor/0/monitor.rbs, line 292
def mon_check_owner: () -> nil
Ensures that the MonitorMixin is owned by the current thread, otherwise raises an exception.
() → nil
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/monitor/0/monitor.rbs, line 209
def mon_enter: () -> nil
Enters exclusive section.
() → nil
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/monitor/0/monitor.rbs, line 217
def mon_exit: () -> nil
Leaves exclusive section.
() → untyped
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/monitor/0/monitor.rbs, line 301
def mon_initialize: () -> untyped
Initializes the MonitorMixin after being included in a class or when an object has been extended with the MonitorMixin
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/monitor/0/monitor.rbs, line 225
def mon_locked?: () -> bool
Returns true if this monitor is locked by any thread
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/monitor/0/monitor.rbs, line 233
def mon_owned?: () -> bool
Returns true if this monitor is locked by current thread.
[T] () { () → T } → T
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/monitor/0/monitor.rbs, line 242
def mon_synchronize: [T] () { () -> T } -> T
Enters exclusive section and executes the block. Leaves the exclusive section automatically when the block exits. See example under MonitorMixin.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/monitor/0/monitor.rbs, line 250
def mon_try_enter: () -> bool
Attempts to enter exclusive section. Returns false if lock fails.
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/stdlib/monitor/0/monitor.rbs, line 259
def new_cond: () -> ::MonitorMixin::ConditionVariable
Creates a new MonitorMixin::ConditionVariable associated with the Monitor object.