class Monitor
当您需要一个用于互斥块的锁定对象时,请使用 Monitor 类。
require 'monitor' lock = Monitor.new lock.synchronize do # exclusive access end
Public Instance Methods
Source
static VALUE
monitor_enter(VALUE monitor)
{
struct monitor_args args;
monitor_args_init(&args, monitor);
monitor_enter0(&args);
return Qnil;
}
进入独占区域。
也别名为: mon_enter
Source
static VALUE
monitor_exit(VALUE monitor)
{
struct monitor_args args;
monitor_args_init(&args, monitor);
monitor_exit0(&args);
return Qnil;
}
离开独占区域。
也别名为: mon_exit
别名为: try_enter
Source
# File ext/monitor/lib/monitor.rb, line 263 def new_cond ::MonitorMixin::ConditionVariable.new(self) end
创建与 Monitor 对象关联的新 MonitorMixin::ConditionVariable。
Source
static VALUE
monitor_synchronize(VALUE monitor)
{
struct monitor_args args;
monitor_args_init(&args, monitor);
monitor_enter0(&args);
return rb_ensure(monitor_sync_body, (VALUE)&args, monitor_sync_ensure, (VALUE)&args);
}
进入独占区域并执行块。块退出时自动离开独占区域。请参阅 MonitorMixin 下的示例。
也别名为: mon_synchronize
Source
static VALUE
monitor_try_enter(VALUE monitor)
{
struct rb_monitor *mc = monitor_ptr(monitor);
VALUE current_fiber = rb_fiber_current();
if (!mc_owner_p(mc, current_fiber)) {
if (!rb_mutex_trylock(mc->mutex)) {
return Qfalse;
}
RB_OBJ_WRITE(monitor, &mc->owner, current_fiber);
mc->count = 0;
}
mc->count += 1;
return Qtrue;
}
尝试进入独占区域。如果锁定失败,则返回 false。
也别名为: try_mon_enter, mon_try_enter
别名为: try_enter