Threadライブラリ

Threadライブラリによってrubyによる並行プログラミングが可能に なります.threadはメモリ空間を共有して同時に実行される制御の 流れです.rubyインタプリタは時分割でthreadを実行しますので, threadを使うことで実行速度が速くなることはありません.

プログラムの開始と同時に生成されるthreadをmain threadと呼び ます.なんらかの理由でmain threadが終了する時には,他の全て のthreadもプログラム全体も終了します.ユーザからの割込みによっ て発生した例外はmain threadに送られます.

The Thread library is optional, and may not be available on some ruby interpreter, which thread feature is disabled by compile time configuration.


Thread

The Thread class represents user-level threads.

Threads terminates when the execution of the given iterater block returns, normally or by raisung an exception.

SuperClass:

Object

Class Methods:

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.

Suspends the current thread until another thread resumes it using run method.

Methods:

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 given to the Thread.create.

Mutex

Mutexs (mutual-exclusion locks) are used to protect shared data against concurrent accesses. The typical use is (where m is the mutex object):

begin
  m.lock
  # critical section protected by m
ensure
  m.unlock
end
or, in short
m.synchronize {
  # critical section protected by m
}

SuperClass:

Object

Class Methods:

new

Creates a new Mutex object.

Methods:

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.

Queue

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.

SuperClass:

Object

Class Methods:

new

Creates a new queue object.

Methods:

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.


up - next - index

matz@caelum.co.jp