Any thread can raise an exception using the raise instance method, which operates
similarly to Kernel#raise.

However, it's important to note that an exception that occurs in any
thread except the main thread depends on abort_on_exception. This
option is false by default, meaning that any unhandled
exception will cause the thread to terminate silently when waited on by
either join or value. You can change this default by
either abort_on_exception=true or setting $DEBUG to true.

With the addition of the class method ::handle_interrupt, you
can now handle exceptions asynchronously with threads.

Ruby provides a few ways to support scheduling threads in your program.

The first way is by using the class method ::stop, to put the current running
thread to sleep and schedule the execution of another thread.

Once a thread is asleep, you can use the instance method wakeup to mark your thread as
eligible for scheduling.

You can also try ::pass, which
attempts to pass execution to another thread but is dependent on the OS
whether a running thread will switch or not. The same goes for priority, which lets you hint to
the thread scheduler which threads you want to take precedence when passing
execution. This method is also dependent on the OS and may be ignored on
some platforms.

Public Class Methods

DEBUG → num
click to toggle source

Returns the thread debug level. Available only if compiled with
THREAD_DEBUG=-1.

handle_interrupt(hash) { ... } → result of the block
click to toggle source

Changes asynchronous interrupt timing.

interrupt means asynchronous event and corresponding procedure by
#raise, #kill, signal trap (not supported yet)
and main thread termination (if main thread terminates, then all other
thread will be killed).

The given hash has pairs like ExceptionClass =>
:TimingSymbol. Where the ExceptionClass is the interrupt handled by
the given block. The TimingSymbol can be one of the following symbols:

:immediate

Invoke interrupts immediately.

:on_blocking

Invoke interrupts while BlockingOperation.

:never

Never invoke all interrupts.

BlockingOperation means that the operation will block the calling
thread, such as read and write. On CRuby implementation,
BlockingOperation is any operation executed without GVL.

Masked asynchronous interrupts are delayed until they are enabled. This
method is similar to sigprocmask(3).

In the next example, we will guard from the TimeoutError exception. This will help
prevent from leaking resources when TimeoutError exceptions occur during
normal ensure clause. For this example we use the help of the standard
library Timeout, from lib/timeout.rb

In the first part of the timeout block, we can rely on TimeoutError being ignored. Then in
the TimeoutError => :on_blocking block, any operation that
will block the calling thread is susceptible to a TimeoutError exception being raised.

Public Instance Methods

thr[sym] → obj or nil
click to toggle source

Attribute Reference—Returns the value of a fiber-local variable (current
thread's root fiber if not explicitly inside a Fiber), using either a symbol or a string name. If
the specified variable does not exist, returns nil.

#[] and #[]= are not thread-local but
fiber-local. This confusion did not exist in Ruby 1.8 because fibers are
only available since Ruby 1.9. Ruby 1.9 chooses that the methods behaves
fiber-local to save following idiom for dynamic scope.

Returns the priority of thr. Default is inherited from the current
thread which creating the new thread, or zero for the initial main thread;
higher-priority thread will run more frequently than lower-priority threads
(but lower-priority threads can also run).

This is just hint for Ruby thread scheduler. It may be ignored on some
platform.