Type:
Module

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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.

broadcast
  • References/Ruby on Rails/Ruby/Classes/MonitorMixin/MonitorMixin::ConditionVariable

broadcast() Instance Public methods Wakes up all threads waiting for this lock

2025-01-10 15:47:30
new
  • References/Ruby on Rails/Ruby/Classes/MonitorMixin

new(*args) Class Public methods Use extend MonitorMixin or include

2025-01-10 15:47:30
extend_object
  • References/Ruby on Rails/Ruby/Classes/MonitorMixin

extend_object(obj) Class Public methods

2025-01-10 15:47:30
try_mon_enter
  • References/Ruby on Rails/Ruby/Classes/MonitorMixin

try_mon_enter() Instance Public methods For backward compatibility

2025-01-10 15:47:30
wait_while
  • References/Ruby on Rails/Ruby/Classes/MonitorMixin/MonitorMixin::ConditionVariable

wait_while() Instance Public methods Calls wait repeatedly while the given block

2025-01-10 15:47:30
new
  • References/Ruby on Rails/Ruby/Classes/MonitorMixin/MonitorMixin::ConditionVariable

new(monitor) Class Public methods

2025-01-10 15:47:30
mon_synchronize
  • References/Ruby on Rails/Ruby/Classes/MonitorMixin

mon_synchronize() Instance Public methods Enters exclusive section and executes

2025-01-10 15:47:30
wait_until
  • References/Ruby on Rails/Ruby/Classes/MonitorMixin/MonitorMixin::ConditionVariable

wait_until() Instance Public methods Calls wait repeatedly until the given block

2025-01-10 15:47:30
signal
  • References/Ruby on Rails/Ruby/Classes/MonitorMixin/MonitorMixin::ConditionVariable

signal() Instance Public methods Wakes up the first thread in line waiting for

2025-01-10 15:47:30
mon_try_enter
  • References/Ruby on Rails/Ruby/Classes/MonitorMixin

mon_try_enter() Instance Public methods Attempts to enter exclusive section

2025-01-10 15:47:30