The lowest-level method of blocking other threads from running uses
a global ``thread critical''
condition.
When the condition is set to
true (using the
Thread.critical=
method),
the scheduler will not schedule any
existing thread to run.
However, this does not block new threads from
being created and run. Certain thread operations (such as stopping or
killing a thread, sleeping in the current thread, or raising an
exception) may cause a thread to be scheduled even when in a critical
section.
Using
Thread.critical=
directly is certainly possible, but it
isn't terribly convenient. Fortunately, Ruby comes packaged with
several alternatives. Of these, two of the best, class
Mutex and
class
ConditionVariable, are available in the
thread library
module; see the documentation beginning on page 457.