The Thread
library allows concurrent programming in Ruby.
It provides multiple threads of control that execute concurrently
sharing same memory space. The Ruby interpreter executes threads by
time-sharing, therefore using threads never makes program run faster
(even much slower for the cost of context switching).
The thread which created first when the Ruby process started, is called the main thread. When the main thread is terminated for some reason, all other threads will be terminated, and the whole process will be terminated. The exception raised by the user interrupt is delivered to the main thread.
The Thread
library is optional, and may not be available
on some Ruby interpreter, which thread feature is disabled by the
compile time configuration.
The Thread
class represents user-level threads.
Threads terminates when the execution of the given iterater block returns, normally or by raisung an exception.
current
Returns the current thread object which calls the method.
exit
Terminates the current thread.
join thread
Suspends the current thread until the specified thread has terminated.
kill(thread)
Terminates the specified thread.
new {...}
start {...}
fork {...}
Creates a new thread of contorl, then starts evaluating the iterator block concurrently. Returns a newly created thread object.
pass
Gives other runnable threads chance to run.
stop
Suspends the current thread until another thread resumes it using
run
method.
alive?
status
Returns TRUE if the thread is alive.
exit
Terminates the thread.
run
Resumes the thread. It does nothing if the thread was not suspended.
stop
Stops the thread until another resumes it.
stop?
Returns true if the thread is stopped.
value
Waits for the thread to terminate and returns the evaluated value of
the iterator block, which is given to the
Thread.create
.
Mutexs (mutual-exclusion locks) are used to protect shared data
against concurrent accesses. The typical use is (where m
is the mutex object):
or, in shortbegin m.lock # critical section protected by m ensure m.unlock end
m.synchronize { # critical section protected by m }
new
Creates a new Mutex
object.
lock
Locks the mutex object. Only on thread at a time can lock the mutex. A thread that attempts to lock a alread locked mutex will suspend until the other thread unlocks the mutex.
locked?
Returns true if the mutex is locked.
synchronize
Locks the mutex, and evaluates the iterator block. Ensures the mutex be unlocked.
try_lock
Locks the mutex and returns true if the mutex is not locked. If the mutex is already locked, just returns false.
unlock
Unlocks the mutex. Other thread suspended trying to lock the mutex will be resumed.
The Queue
is the FIFO (first in first out) communication
channel between threads. If a thread trys to read an empty queue, it
will be suspended until the queue is filled.
new
Creates a new queue object.
empty?
Returns true if the queue is empty.
pop [non_block]
Removes and returns an value from queue. If the queue is empty, the
calling thread will be suspended until some value pushed in the queue.
If optional argument non_block is non-nil, pop
raises an exception if no value available in the queue.
push(value
)
Append an value to the queue. Restart the waiting thread if any.