非常教程

Ruby 2.4参考手册

监控 | Monitor

MonitorMixin

在并发编程中,监视器是多个线程安全使用的对象或模块。监视器的定义特征是它的方法在互斥的情况下执行。也就是说,在每个时间点,最多一个线程可能正在执行其任何方法。与推断更新数据结构的并行代码相比,这种互斥极大地简化了关于监视器实现的推理。

您可以阅读关于监视器维基百科页面上的一般原理的更多信息

例子

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

消费者线程等待生产者线程将行推送到buf buf.empty?。生产者线程(主线程)从ARGF读取一行并将其推入buf,然后调用empty_cond.signal以通知消费者线程新数据。

简单类包括

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实现具有对项目的同步访问权限的数组。该类是作为包含MonitorMixin模块的Array的子​​类实现的。

公共类方法

extend_object(obj) Show source

调用超类方法

# File lib/monitor.rb, line 159
def self.extend_object(obj)
  super(obj)
  obj.__send__(:mon_initialize)
end

new(*args) Show source

使用extend MonitorMixininclude MonitorMixin代替这个构造函数。看看上面的例子来理解如何使用这个模块。

调用超类方法

# File lib/monitor.rb, line 233
def initialize(*args)
  super
  mon_initialize
end

公共实例方法

mon_enter() Show source

进入专属部分。

# File lib/monitor.rb, line 184
def mon_enter
  if @mon_owner != Thread.current
    @mon_mutex.lock
    @mon_owner = Thread.current
    @mon_count = 0
  end
  @mon_count += 1
end

mon_exit() Show source

留下专属部分。

# File lib/monitor.rb, line 196
def mon_exit
  mon_check_owner
  @mon_count -=1
  if @mon_count == 0
    @mon_owner = nil
    @mon_mutex.unlock
  end
end

mon_synchronize() { || ... } Show source

进入独占部分并执行该块。块停止时自动保留专用段。见下例MonitorMixin

# File lib/monitor.rb, line 210
def mon_synchronize
  mon_enter
  begin
    yield
  ensure
    mon_exit
  end
end

Also aliased as: synchronize

mon_try_enter() Show source

尝试输入专属部分。false锁定失败时返回。

# File lib/monitor.rb, line 167
def mon_try_enter
  if @mon_owner != Thread.current
    unless @mon_mutex.try_lock
      return false
    end
    @mon_owner = Thread.current
    @mon_count = 0
  end
  @mon_count += 1
  return true
end

Also aliased as: try_mon_enter

new_cond() Show source

Creates a new MonitorMixin::ConditionVariable associated with the receiver.

# File lib/monitor.rb, line 224
def new_cond
  return ConditionVariable.new(self)
end

synchronize()

Alias for: mon_synchronize

try_mon_enter()

For backward compatibility

Alias for: mon_try_enter

私有实例方法

mon_check_owner() Show source

# File lib/monitor.rb, line 246
def mon_check_owner
  if @mon_owner != Thread.current
    raise ThreadError, "current thread not owner"
  end
end

mon_enter_for_cond(count) Show source

# File lib/monitor.rb, line 252
def mon_enter_for_cond(count)
  @mon_owner = Thread.current
  @mon_count = count
end

mon_exit_for_cond() Show source

# File lib/monitor.rb, line 257
def mon_exit_for_cond
  count = @mon_count
  @mon_owner = nil
  @mon_count = 0
  return count
end

mon_initialize() Show source

在包含在类中或 MonitorMixin 扩展了对象后初始化MonitorMixin

# File lib/monitor.rb, line 240
def mon_initialize
  @mon_owner = nil
  @mon_count = 0
  @mon_mutex = Thread::Mutex.new
end
Ruby 2.4

Ruby 是一种面向对象、命令式、函数式、动态的通用编程语言,是世界上最优美而巧妙的语言。

主页 https://www.ruby-lang.org/
源码 https://github.com/ruby/ruby
版本 2.4
发布版本 2.4.1

Ruby 2.4目录

1.缩略 | Abbrev
2.ARGF
3.数组 | Array
4.Base64
5.基本对象 | BasicObject
6.基准测试 | Benchmark
7.BigDecimal
8.绑定 | Binding
9.CGI
10.类 | Class
11.比较 | Comparable
12.负责 | Complex
13.计算续体 | Continuation
14.覆盖 | Coverage
15.CSV
16.日期 | Date
17.日期时间 | DateTime
18.DBM
19.代理 | Delegator
20.摘要 | Digest
21.Dir
22.DRb
23.编码 | Encoding
24.枚举 | Enumerable
25.枚举 | Enumerator
26.ENV
27.ERB
28.错误 | Errors
29.Etc
30.期望值 | Exception
31.错误类 | FalseClass
32.Fiber
33.Fiddle
34.文件 | File
35.文件实用程序 | FileUtils
36.查找 | Find
37.浮点 | Float
38.Forwardable
39.GC
40.GDBM
41.GetoptLong
42.Hash
43.Integer
44.IO
45.IPAddr
46.JSON
47.Kernel
48.语言 | 3Language
49.记录 | Logger
50.编排 | Marshal
51.MatchData
52.数学 | Math
53.矩阵 | Matrix
54.方法 | Method
55.模型 | Module
56.监控 | Monitor
57. 互斥 | Mutex
58.Net
59.Net::FTP
60.Net::HTTP
61.Net::IMAP
62.Net::SMTP
63.NilClass
64.数字 | Numeric
65.对象 | Object
66.ObjectSpace
67.Observable
68.Open3
69.OpenSSL
70.OpenStruct
71.OpenURI
72.OptionParser
73.路径名 | Pathname
74.完整输出 | PrettyPrint
75.Prime
76.Proc
77.过程 | Process
78.PStore
79.PTY
80.队列 | Queue
81.随机 | Random
82.范围 | Range
83.合理的 | Rational
84.Readline
85.Regexp
86.Resolv
87.Ripper
88.RubyVM
89.Scanf
90.SDBM
91.SecureRandom
92.Set
93.Shell
94.信号 | Signal
95.Singleton
96.套接字 | Socket
97.字符串 | String
98.StringIO
99.StringScanner
100.结构 | Struct