Class: Thread
Overview
Threads are the Ruby implementation for a concurrent programming model.
Programs that require multiple threads of execution are a perfect candidate for Ruby’s Thread class.
For example, we can create a new thread separate from the main thread’s execution using ::new.
thr = Thread.new { puts "What's the big deal" }
Then we are able to pause the execution of the main thread and allow our new thread to finish, using #join:
thr.join #=> "What's the big deal"
If we don’t call thr.join
before the main thread terminates, then all other threads including thr
will be killed.
Alternatively, you can use an array for handling multiple threads at once, like in the following example:
threads = []
threads << Thread.new { puts "What's the big deal" }
threads << Thread.new { 3.times { puts "Threads are fun!" } }
After creating a few threads we wait for them all to finish consecutively.
threads.each { |thr| thr.join }
To retrieve the last value of a thread, use #value
thr = Thread.new { sleep 1; "Useful value" }
thr.value #=> "Useful value"
Thread initialization
In order to create new threads, Ruby provides ::new, ::start, and ::fork. A block must be provided with each of these methods, otherwise a ThreadError will be raised.
When subclassing the Thread class, the initialize
method of your subclass will be ignored by ::start and ::fork. Otherwise, be sure to call super in your initialize
method.
=== Thread termination
For terminating threads, Ruby provides a variety of ways to do this.
The class method ::kill, is meant to exit a given thread:
thr = Thread.new { sleep }
Thread.kill(thr) # sends exit() to thr
Alternatively, you can use the instance method #exit, or any of its aliases #kill or #terminate.
thr.exit
=== Thread status
Ruby provides a few instance methods for querying the state of a given thread. To get a string with the current thread’s state use #status
thr = Thread.new { sleep }
thr.status # => "sleep"
thr.exit
thr.status # => false
You can also use #alive? to tell if the thread is running or sleeping, and #stop? if the thread is dead or sleeping.
=== Thread variables and scope
Since threads are created with blocks, the same rules apply to other Ruby blocks for variable scope. Any local variables created within this block are accessible to only this thread.
==== Fiber-local vs. Thread-local
Each fiber has its own bucket for Thread#[] storage. When you set a new fiber-local it is only accessible within this Fiber. To illustrate:
Thread.new {
Thread.current[:foo] = "bar"
Fiber.new {
p Thread.current[:foo] # => nil
}.resume
}.join
This example uses #[] for getting and #[]= for setting fiber-locals, you can also use #keys to list the fiber-locals for a given thread and #key? to check if a fiber-local exists.
When it comes to thread-locals, they are accessible within the entire scope of the thread. Given the following example:
Thread.new{
Thread.current.thread_variable_set(:foo, 1)
p Thread.current.thread_variable_get(:foo) # => 1
Fiber.new{
Thread.current.thread_variable_set(:foo, 2) p Thread.current.thread_variable_get(:foo) # => 2
}.resume
p Thread.current.thread_variable_get(:foo) # => 2
}.join
You can see that the thread-local +:foo+ carried over into the fiber
and was changed to +2+ by the end of the thread.
This example makes use of #thread_variable_set to create new
thread-locals, and #thread_variable_get to reference them.
There is also #thread_variables to list all thread-locals, and
#thread_variable? to check if a given thread-local exists.
=== Exception handling
When an unhandled exception is raised inside a thread, it will
terminate. By default, this exception will not propagate to other
threads. The exception is stored and when another thread calls #value
or #join, the exception will be re-raised in that thread.
t = Thread.new{ raise 'something went wrong' }
t.value #=> RuntimeError: something went wrong
An exception can be raised from outside the thread using the
Thread#raise instance method, which takes the same parameters as
Kernel#raise.
Setting Thread.abort_on_exception = true, Thread#abort_on_exception =
true, or $DEBUG = true will cause a subsequent unhandled exception
raised in a thread to be automatically re-raised in the main thread.
With the addition of the class method ::handle_interrupt, you can now handle exceptions asynchronously with threads.
=== Scheduling
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.
Direct Known Subclasses
Defined Under Namespace
Classes: Backtrace
Class Method Summary collapse
-
.abort_on_exception ⇒ Boolean
Returns the status of the global “abort on exception” condition.
-
.abort_on_exception=(boolean) ⇒ Boolean
When set to
true
, if any thread is aborted by an exception, the raised exception will be re-raised in the main thread. -
.current ⇒ Object
Returns the currently executing thread.
-
.DEBUG ⇒ Numeric
Returns the thread debug level.
-
.DEBUG=(num) ⇒ Object
Sets the thread debug level.
-
.exit ⇒ Object
Terminates the currently running thread and schedules another thread to be run.
-
.fork(args) ⇒ Object
Basically the same as ::new.
-
.handle_interrupt(hash) { ... } ⇒ Object
Changes asynchronous interrupt timing.
-
.kill(thread) ⇒ Object
Causes the given
thread
to exit, see also Thread::exit. -
.list ⇒ Array
Returns an array of Thread objects for all threads that are either runnable or stopped.
-
.main ⇒ Object
Returns the main thread.
-
.new(*args) ⇒ Object
Thread.new(*args, &proc) -> thread Thread.new(*args) { |args| … } -> thread.
-
.pass ⇒ nil
Give the thread scheduler a hint to pass execution to another thread.
-
.pending_interrupt?(error = nil) ⇒ Boolean
Returns whether or not the asynchronous queue is empty.
-
.report_on_exception ⇒ Boolean
Returns the status of the global “report on exception” condition.
-
.report_on_exception=(boolean) ⇒ Boolean
Returns the new state.
-
.start(args) ⇒ Object
Basically the same as ::new.
-
.stop ⇒ nil
Stops execution of the current thread, putting it into a “sleep” state, and schedules execution of another thread.
Instance Method Summary collapse
-
#[](sym) ⇒ Object?
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.
-
#[]=(sym) ⇒ Object
Attribute Assignment—Sets or creates the value of a fiber-local variable, using either a symbol or a string.
-
#abort_on_exception ⇒ Boolean
Returns the status of the thread-local “abort on exception” condition for this
thr
. -
#abort_on_exception=(boolean) ⇒ Boolean
When set to
true
, if thisthr
is aborted by an exception, the raised exception will be re-raised in the main thread. -
#add_trace_func(proc) ⇒ Proc
Adds proc as a handler for tracing.
-
#alive? ⇒ Boolean
Returns
true
ifthr
is running or sleeping. -
#backtrace ⇒ Array
Returns the current backtrace of the target thread.
-
#backtrace_locations(*args) ⇒ Object
Returns the execution stack for the target thread—an array containing backtrace location objects.
-
#exit ⇒ Object
Terminates
thr
and schedules another thread to be run, returning the terminated Thread. -
#fetch(*args) ⇒ Object
Returns a fiber-local for the given key.
-
#group ⇒ nil
Returns the ThreadGroup which contains the given thread, or returns
nil
ifthr
is not a member of any group. -
#initialize(args) ⇒ Object
constructor
:nodoc:.
-
#join(*args) ⇒ Object
The calling thread will suspend execution and run this
thr
. -
#key?(sym) ⇒ Boolean
Returns
true
if the given string (or symbol) exists as a fiber-local variable. -
#keys ⇒ Array
Returns an array of the names of the fiber-local variables (as Symbols).
-
#kill ⇒ Object
Terminates
thr
and schedules another thread to be run, returning the terminated Thread. -
#name ⇒ String
show the name of the thread.
-
#name=(name) ⇒ String
set given name to the ruby thread.
-
#pending_interrupt?(error = nil) ⇒ Boolean
Returns whether or not the asynchronous queue is empty for the target thread.
-
#priority ⇒ Integer
Returns the priority of thr.
-
#priority=(integer) ⇒ Object
Sets the priority of thr to integer.
-
#raise(*args) ⇒ Object
Raises an exception from the given thread.
-
#report_on_exception ⇒ Boolean
Returns the status of the thread-local “report on exception” condition for this
thr
. -
#report_on_exception=(boolean) ⇒ Boolean
When set to
true
, a message is printed on $stderr if an exception kills thisthr
. -
#run ⇒ Object
Wakes up
thr
, making it eligible for scheduling. -
#safe_level ⇒ Integer
Returns the safe level.
-
#set_trace_func(trace) ⇒ Object
Establishes proc on thr as the handler for tracing, or disables tracing if the parameter is
nil
. -
#status ⇒ String, ...
Returns the status of
thr
. -
#stop? ⇒ Boolean
Returns
true
ifthr
is dead or sleeping. -
#terminate ⇒ Object
Terminates
thr
and schedules another thread to be run, returning the terminated Thread. -
#thread_variable?(key) ⇒ Boolean
Returns
true
if the given string (or symbol) exists as a thread-local variable. -
#thread_variable_get(key) ⇒ Object?
Returns the value of a thread local variable that has been set.
-
#thread_variable_set(key, value) ⇒ Object
Sets a thread local with
key
tovalue
. -
#thread_variables ⇒ Array
Returns an array of the names of the thread-local variables (as Symbols).
-
#to_s ⇒ String
(also: #inspect)
Dump the name, id, and status of thr to a string.
-
#value ⇒ Object
Waits for
thr
to complete, using #join, and returns its value or raises the exception which terminated the thread. -
#wakeup ⇒ Object
Marks a given thread as eligible for scheduling, however it may still remain blocked on I/O.
Constructor Details
#initialize(args) ⇒ Object
:nodoc:
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 |
# File 'thread.c', line 941
static VALUE
thread_initialize(VALUE thread, VALUE args)
{
rb_thread_t *th = rb_thread_ptr(thread);
if (!rb_block_given_p()) {
rb_raise(rb_eThreadError, "must be called with a block");
}
else if (th->invoke_type != thread_invoke_type_none) {
VALUE loc = threadptr_invoke_proc_location(th);
if (!NIL_P(loc)) {
rb_raise(rb_eThreadError,
"already initialized thread - %"PRIsVALUE":%"PRIsVALUE,
RARRAY_AREF(loc, 0), RARRAY_AREF(loc, 1));
}
else {
rb_raise(rb_eThreadError, "already initialized thread");
}
}
else {
return thread_create_core(thread, args, NULL);
}
}
|
Class Method Details
.abort_on_exception ⇒ Boolean
Returns the status of the global “abort on exception” condition.
The default is false
.
When set to true
, if any thread is aborted by an exception, the raised exception will be re-raised in the main thread.
Can also be specified by the global $DEBUG flag or command line option -d
.
See also ::abort_on_exception=.
There is also an instance level method to set this for a specific thread, see #abort_on_exception.
2736 2737 2738 2739 2740 |
# File 'thread.c', line 2736
static VALUE
rb_thread_s_abort_exc(VALUE _)
{
return GET_THREAD()->vm->thread_abort_on_exception ? Qtrue : Qfalse;
}
|
.abort_on_exception=(boolean) ⇒ Boolean
When set to true
, if any thread is aborted by an exception, the raised exception will be re-raised in the main thread. Returns the new state.
Thread.abort_on_exception = true
t1 = Thread.new do
puts "In new thread"
raise "Exception from thread"
end
sleep(1)
puts "not reached"
This will produce:
In new thread
prog.rb:4: Exception from thread (RuntimeError)
from prog.rb:2:in `initialize'
from prog.rb:2:in `new'
from prog.rb:2
See also ::abort_on_exception.
There is also an instance level method to set this for a specific thread, see #abort_on_exception=.
2773 2774 2775 2776 2777 2778 |
# File 'thread.c', line 2773
static VALUE
rb_thread_s_abort_exc_set(VALUE self, VALUE val)
{
GET_THREAD()->vm->thread_abort_on_exception = RTEST(val);
return val;
}
|
.current ⇒ Object
Returns the currently executing thread.
Thread.current #=> #<Thread:0x401bdf4c run>
2690 2691 2692 2693 2694 |
# File 'thread.c', line 2690
static VALUE
thread_s_current(VALUE klass)
{
return rb_thread_current();
}
|
.DEBUG ⇒ Numeric
Returns the thread debug level. Available only if compiled with THREAD_DEBUG=-1.
306 307 308 309 310 |
# File 'thread.c', line 306
static VALUE
rb_thread_s_debug(void)
{
return INT2NUM(rb_thread_debug_enabled);
}
|
.DEBUG=(num) ⇒ Object
Sets the thread debug level. Available only if compiled with THREAD_DEBUG=-1.
320 321 322 323 324 325 |
# File 'thread.c', line 320
static VALUE
rb_thread_s_debug_set(VALUE self, VALUE val)
{
rb_thread_debug_enabled = RTEST(val) ? NUM2INT(val) : 0;
return val;
}
|
.exit ⇒ Object
Terminates the currently running thread and schedules another thread to be run.
If this thread is already marked to be killed, ::exit returns the Thread.
If this is the main thread, or the last thread, exit the process.
2515 2516 2517 2518 2519 2520 |
# File 'thread.c', line 2515
static VALUE
rb_thread_exit(VALUE _)
{
rb_thread_t *th = GET_THREAD();
return rb_thread_kill(th->self);
}
|
.start([args]) {|args| ... } ⇒ Object .fork([args]) {|args| ... } ⇒ Object
Basically the same as ::new. However, if class Thread is subclassed, then calling start
in that subclass will not invoke the subclass’s initialize
method.
923 924 925 926 927 |
# File 'thread.c', line 923
static VALUE
thread_start(VALUE klass, VALUE args)
{
return thread_create_core(rb_thread_alloc(klass), args, 0);
}
|
.handle_interrupt(hash) { ... } ⇒ Object
Changes asynchronous interrupt timing.
interrupt means asynchronous event and corresponding procedure by Thread#raise, Thread#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).
NOTE
Asynchronous interrupts are difficult to use.
If you need to communicate between threads, please consider to use another way such as Queue.
Or use them with deep understanding about this method.
Usage
In this example, we can guard from Thread#raise exceptions.
Using the :never
TimingSymbol the RuntimeError exception will always be ignored in the first block of the main thread. In the second ::handle_interrupt block we can purposefully handle RuntimeError exceptions.
th = Thread.new do
Thread.handle_interrupt(RuntimeError => :never) {
begin
# You can write resource allocation code safely.
Thread.handle_interrupt(RuntimeError => :immediate) {
# ...
}
ensure
# You can write resource deallocation code safely.
end
}
end
Thread.pass
# ...
th.raise "stop"
While we are ignoring the RuntimeError exception, it’s safe to write our resource allocation code. Then, the ensure block is where we can safely deallocate your resources.
Guarding from Timeout::Error
In the next example, we will guard from the Timeout::Error exception. This will help prevent from leaking resources when Timeout::Error exceptions occur during normal ensure clause. For this example we use the help of the standard library Timeout, from lib/timeout.rb
require 'timeout'
Thread.handle_interrupt(Timeout::Error => :never) {
timeout(10){
# Timeout::Error doesn't occur here
Thread.handle_interrupt(Timeout::Error => :on_blocking) {
# possible to be killed by Timeout::Error
# while blocking operation
}
# Timeout::Error doesn't occur here
}
}
In the first part of the timeout
block, we can rely on Timeout::Error being ignored. Then in the Timeout::Error => :on_blocking
block, any operation that will block the calling thread is susceptible to a Timeout::Error exception being raised.
Stack control settings
It’s possible to stack multiple levels of ::handle_interrupt blocks in order to control more than one ExceptionClass and TimingSymbol at a time.
Thread.handle_interrupt(FooError => :never) {
Thread.handle_interrupt(BarError => :never) {
# FooError and BarError are prohibited.
}
}
Inheritance with ExceptionClass
All exceptions inherited from the ExceptionClass parameter will be considered.
Thread.handle_interrupt(Exception => :never) {
# all exceptions inherited from Exception are prohibited.
}
2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 |
# File 'thread.c', line 2019
static VALUE
rb_thread_s_handle_interrupt(VALUE self, VALUE mask_arg)
{
VALUE mask;
rb_execution_context_t * volatile ec = GET_EC();
rb_thread_t * volatile th = rb_ec_thread_ptr(ec);
volatile VALUE r = Qnil;
enum ruby_tag_type state;
if (!rb_block_given_p()) {
rb_raise(rb_eArgError, "block is needed.");
}
mask = 0;
mask_arg = rb_to_hash_type(mask_arg);
rb_hash_foreach(mask_arg, handle_interrupt_arg_check_i, (VALUE)&mask);
if (!mask) {
return rb_yield(Qnil);
}
OBJ_FREEZE_RAW(mask);
rb_ary_push(th->pending_interrupt_mask_stack, mask);
if (!rb_threadptr_pending_interrupt_empty_p(th)) {
th->pending_interrupt_queue_checked = 0;
RUBY_VM_SET_INTERRUPT(th->ec);
}
EC_PUSH_TAG(th->ec);
if ((state = EC_EXEC_TAG()) == TAG_NONE) {
r = rb_yield(Qnil);
}
EC_POP_TAG();
rb_ary_pop(th->pending_interrupt_mask_stack);
if (!rb_threadptr_pending_interrupt_empty_p(th)) {
th->pending_interrupt_queue_checked = 0;
RUBY_VM_SET_INTERRUPT(th->ec);
}
RUBY_VM_CHECK_INTS(th->ec);
if (state) {
EC_JUMP_TAG(th->ec, state);
}
return r;
}
|
.kill(thread) ⇒ Object
2496 2497 2498 2499 2500 |
# File 'thread.c', line 2496
static VALUE
rb_thread_s_kill(VALUE obj, VALUE th)
{
return rb_thread_kill(th);
}
|
.list ⇒ Array
Returns an array of Thread objects for all threads that are either runnable or stopped.
Thread.new { sleep(200) }
Thread.new { 1000000.times {|i| i*i } }
Thread.new { Thread.stop }
Thread.list.each {|t| p t}
This will produce:
#<Thread:0x401b3e84 sleep>
#<Thread:0x401b3f38 run>
#<Thread:0x401b3fb0 sleep>
#<Thread:0x401bdf4c run>
2669 2670 2671 2672 2673 |
# File 'thread.c', line 2669
static VALUE
thread_list(VALUE _)
{
return rb_thread_list();
}
|
.main ⇒ Object
Returns the main thread.
2709 2710 2711 2712 2713 |
# File 'thread.c', line 2709
static VALUE
rb_thread_s_main(VALUE klass)
{
return rb_thread_main();
}
|
.new(*args) ⇒ Object
Thread.new(*args, &proc) -> thread
Thread.new(*args) { |args| ... } -> thread
Creates a new thread executing the given block.
Any +args+ given to ::new will be passed to the block:
arr = [] a, b, c = 1, 2, 3 Thread.new(a,b,c) { |d,e,f| arr << d << e << f }.join arr #=> [1, 2, 3]
A ThreadError exception is raised if ::new is called without a block.
If you're going to subclass Thread, be sure to call super in your
+initialize+ method, otherwise a ThreadError will be raised.
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 |
# File 'thread.c', line 895
static VALUE
thread_s_new(int argc, VALUE *argv, VALUE klass)
{
rb_thread_t *th;
VALUE thread = rb_thread_alloc(klass);
if (GET_VM()->main_thread->status == THREAD_KILLED)
rb_raise(rb_eThreadError, "can't alloc thread");
rb_obj_call_init_kw(thread, argc, argv, RB_PASS_CALLED_KEYWORDS);
th = rb_thread_ptr(thread);
if (!threadptr_initialized(th)) {
rb_raise(rb_eThreadError, "uninitialized thread - check `%"PRIsVALUE"#initialize'",
klass);
}
return thread;
}
|
.pass ⇒ nil
Give the thread scheduler a hint to pass execution to another thread. A running thread may or may not switch, it depends on OS and processor.
1725 1726 1727 1728 1729 1730 |
# File 'thread.c', line 1725
static VALUE
thread_s_pass(VALUE klass)
{
rb_thread_schedule();
return Qnil;
}
|
.pending_interrupt?(error = nil) ⇒ Boolean
Returns whether or not the asynchronous queue is empty.
Since Thread::handle_interrupt can be used to defer asynchronous events, this method can be used to determine if there are any deferred events.
If you find this method returns true, then you may finish :never
blocks.
For example, the following method processes deferred asynchronous events immediately.
def Thread.kick_interrupt_immediately
Thread.handle_interrupt(Object => :immediate) {
Thread.pass
}
end
If error
is given, then check only for error
type deferred events.
Usage
th = Thread.new{
Thread.handle_interrupt(RuntimeError => :on_blocking){
while true
...
# reach safe point to invoke interrupt
if Thread.pending_interrupt?
Thread.handle_interrupt(Object => :immediate){}
end
...
end
}
}
...
th.raise # stop thread
This example can also be written as the following, which you should use to avoid asynchronous interrupts.
flag = true
th = Thread.new{
Thread.handle_interrupt(RuntimeError => :on_blocking){
while true
...
# reach safe point to invoke interrupt
break if flag == false
...
end
}
}
...
flag = false # stop thread
2161 2162 2163 2164 2165 |
# File 'thread.c', line 2161
static VALUE
rb_thread_s_pending_interrupt_p(int argc, VALUE *argv, VALUE self)
{
return rb_thread_pending_interrupt_p(argc, argv, GET_THREAD()->self);
}
|
.report_on_exception ⇒ Boolean
Returns the status of the global “report on exception” condition.
The default is true
since Ruby 2.5.
All threads created when this flag is true will report a message on $stderr if an exception kills the thread.
Thread.new { 1.times { raise } }
will produce this output on $stderr:
#<Thread:...> terminated with exception (report_on_exception is true):
Traceback (most recent call last):
2: from -e:1:in `block in <main>'
1: from -e:1:in `times'
This is done to catch errors in threads early. In some cases, you might not want this output. There are multiple ways to avoid the extra output:
-
If the exception is not intended, the best is to fix the cause of the exception so it does not happen anymore.
-
If the exception is intended, it might be better to rescue it closer to where it is raised rather then let it kill the Thread.
-
If it is guaranteed the Thread will be joined with Thread#join or Thread#value, then it is safe to disable this report with
Thread.current.report_on_exception = false
when starting the Thread. However, this might handle the exception much later, or not at all if the Thread is never joined due to the parent thread being blocked, etc.
See also ::report_on_exception=.
There is also an instance level method to set this for a specific thread, see #report_on_exception=.
2866 2867 2868 2869 2870 |
# File 'thread.c', line 2866
static VALUE
rb_thread_s_report_exc(VALUE _)
{
return GET_THREAD()->vm->thread_report_on_exception ? Qtrue : Qfalse;
}
|
.report_on_exception=(boolean) ⇒ Boolean
Returns the new state. When set to true
, all threads created afterwards will inherit the condition and report a message on $stderr if an exception kills a thread:
Thread.report_on_exception = true
t1 = Thread.new do
puts "In new thread"
raise "Exception from thread"
end
sleep(1)
puts "In the main thread"
This will produce:
In new thread
#<Thread:...prog.rb:2> terminated with exception (report_on_exception is true):
Traceback (most recent call last):
prog.rb:4:in `block in <main>': Exception from thread (RuntimeError)
In the main thread
See also ::report_on_exception.
There is also an instance level method to set this for a specific thread, see #report_on_exception=.
2903 2904 2905 2906 2907 2908 |
# File 'thread.c', line 2903
static VALUE
rb_thread_s_report_exc_set(VALUE self, VALUE val)
{
GET_THREAD()->vm->thread_report_on_exception = RTEST(val);
return val;
}
|
.start([args]) {|args| ... } ⇒ Object .fork([args]) {|args| ... } ⇒ Object
Basically the same as ::new. However, if class Thread is subclassed, then calling start
in that subclass will not invoke the subclass’s initialize
method.
923 924 925 926 927 |
# File 'thread.c', line 923
static VALUE
thread_start(VALUE klass, VALUE args)
{
return thread_create_core(rb_thread_alloc(klass), args, 0);
}
|
.stop ⇒ nil
2621 2622 2623 2624 2625 |
# File 'thread.c', line 2621
static VALUE
thread_stop(VALUE _)
{
return rb_thread_stop();
}
|
Instance Method Details
#[](sym) ⇒ Object?
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
.
[
Thread.new { Thread.current["name"] = "A" },
Thread.new { Thread.current[:name] = "B" },
Thread.new { Thread.current["name"] = "C" }
].each do |th|
th.join
puts "#{th.inspect}: #{th[:name]}"
end
This will produce:
#<Thread:0x00000002a54220 dead>: A
#<Thread:0x00000002a541a8 dead>: B
#<Thread:0x00000002a54130 dead>: C
Thread#[] and Thread#[]= 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.
def meth(newvalue)
begin
oldvalue = Thread.current[:name]
Thread.current[:name] = newvalue
yield
ensure
Thread.current[:name] = oldvalue
end
end
The idiom may not work as dynamic scope if the methods are thread-local and a given block switches fiber.
f = Fiber.new {
meth(1) {
Fiber.yield
}
}
meth(2) {
f.resume
}
f.resume
p Thread.current[:name]
#=> nil if fiber-local
#=> 2 if thread-local (The value 2 is leaked to outside of meth method.)
For thread-local variables, please see #thread_variable_get and #thread_variable_set.
3281 3282 3283 3284 3285 3286 3287 |
# File 'thread.c', line 3281
static VALUE
rb_thread_aref(VALUE thread, VALUE key)
{
ID id = rb_check_id(&key);
if (!id) return Qnil;
return rb_thread_local_aref(thread, id);
}
|
#[]=(sym) ⇒ Object
Attribute Assignment—Sets or creates the value of a fiber-local variable, using either a symbol or a string.
See also Thread#[].
For thread-local variables, please see #thread_variable_set and #thread_variable_get.
3386 3387 3388 3389 3390 |
# File 'thread.c', line 3386
static VALUE
rb_thread_aset(VALUE self, VALUE id, VALUE val)
{
return rb_thread_local_aset(self, rb_to_id(id), val);
}
|
#abort_on_exception ⇒ Boolean
Returns the status of the thread-local “abort on exception” condition for this thr
.
The default is false
.
See also #abort_on_exception=.
There is also a class level method to set this for all threads, see ::abort_on_exception.
2796 2797 2798 2799 2800 |
# File 'thread.c', line 2796
static VALUE
rb_thread_abort_exc(VALUE thread)
{
return rb_thread_ptr(thread)->abort_on_exception ? Qtrue : Qfalse;
}
|
#abort_on_exception=(boolean) ⇒ Boolean
When set to true
, if this thr
is aborted by an exception, the raised exception will be re-raised in the main thread.
See also #abort_on_exception.
There is also a class level method to set this for all threads, see ::abort_on_exception=.
2816 2817 2818 2819 2820 2821 |
# File 'thread.c', line 2816
static VALUE
rb_thread_abort_exc_set(VALUE thread, VALUE val)
{
rb_thread_ptr(thread)->abort_on_exception = RTEST(val);
return val;
}
|
#add_trace_func(proc) ⇒ Proc
Adds proc as a handler for tracing.
See Thread#set_trace_func and Kernel#set_trace_func.
551 552 553 554 555 556 |
# File 'vm_trace.c', line 551
static VALUE
thread_add_trace_func_m(VALUE obj, VALUE trace)
{
thread_add_trace_func(GET_EC(), rb_thread_ptr(obj), trace);
return trace;
}
|
#alive? ⇒ Boolean
3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 |
# File 'thread.c', line 3061
static VALUE
rb_thread_alive_p(VALUE thread)
{
if (rb_threadptr_dead(rb_thread_ptr(thread))) {
return Qfalse;
}
else {
return Qtrue;
}
}
|
#backtrace ⇒ Array
Returns the current backtrace of the target thread.
5123 5124 5125 5126 5127 |
# File 'thread.c', line 5123
static VALUE
rb_thread_backtrace_m(int argc, VALUE *argv, VALUE thval)
{
return rb_vm_thread_backtrace(argc, argv, thval);
}
|
#backtrace_locations(*args) ⇒ Object
Returns the execution stack for the target thread—an array containing backtrace location objects.
See Thread::Backtrace::Location for more information.
This method behaves similarly to Kernel#caller_locations except it applies to a specific thread.
5140 5141 5142 5143 5144 |
# File 'thread.c', line 5140
static VALUE
rb_thread_backtrace_locations_m(int argc, VALUE *argv, VALUE thval)
{
return rb_vm_thread_backtrace_locations(argc, argv, thval);
}
|
#exit ⇒ Object #kill ⇒ Object #terminate ⇒ Object
Terminates thr
and schedules another thread to be run, returning the terminated Thread. If this is the main thread, or the last thread, exits the process.
2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 |
# File 'thread.c', line 2445
VALUE
rb_thread_kill(VALUE thread)
{
rb_thread_t *th = rb_thread_ptr(thread);
if (th->to_kill || th->status == THREAD_KILLED) {
return thread;
}
if (th == th->vm->main_thread) {
rb_exit(EXIT_SUCCESS);
}
thread_debug("rb_thread_kill: %p (%"PRI_THREAD_ID")\n", (void *)th, thread_id_str(th));
if (th == GET_THREAD()) {
/* kill myself immediately */
rb_threadptr_to_kill(th);
}
else {
threadptr_check_pending_interrupt_queue(th);
rb_threadptr_pending_interrupt_enque(th, eKillSignal);
rb_threadptr_interrupt(th);
}
return thread;
}
|
#fetch(sym) ⇒ Object #fetch(sym) { ... } ⇒ Object #fetch(sym, default) ⇒ Object
Returns a fiber-local for the given key. If the key can’t be found, there are several options: With no other arguments, it will raise a KeyError exception; if default is given, then that will be returned; if the optional code block is specified, then that will be run and its result returned. See Thread#[] and Hash#fetch.
3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 |
# File 'thread.c', line 3302
static VALUE
rb_thread_fetch(int argc, VALUE *argv, VALUE self)
{
VALUE key, val;
ID id;
rb_thread_t *target_th = rb_thread_ptr(self);
int block_given;
rb_check_arity(argc, 1, 2);
key = argv[0];
block_given = rb_block_given_p();
if (block_given && argc == 2) {
rb_warn("block supersedes default value argument");
}
id = rb_check_id(&key);
if (id == recursive_key) {
return target_th->ec->local_storage_recursive_hash;
}
else if (id && target_th->ec->local_storage &&
st_lookup(target_th->ec->local_storage, id, &val)) {
return val;
}
else if (block_given) {
return rb_yield(key);
}
else if (argc == 1) {
rb_key_err_raise(rb_sprintf("key not found: %+"PRIsVALUE, key), self, key);
}
else {
return argv[1];
}
}
|
#group ⇒ nil
2965 2966 2967 2968 2969 2970 |
# File 'thread.c', line 2965
VALUE
rb_thread_group(VALUE thread)
{
VALUE group = rb_thread_ptr(thread)->thgroup;
return group == 0 ? Qnil : group;
}
|
#join ⇒ Object #join(limit) ⇒ Object
The calling thread will suspend execution and run this thr
.
Does not return until thr
exits or until the given limit
seconds have passed.
If the time limit expires, nil
will be returned, otherwise thr
is returned.
Any threads not joined will be killed when the main program exits.
If thr
had previously raised an exception and the ::abort_on_exception or $DEBUG flags are not set, (so the exception has not yet been processed), it will be processed at this time.
a = Thread.new { print "a"; sleep(10); print "b"; print "c" }
x = Thread.new { print "x"; Thread.pass; print "y"; print "z" }
x.join # Let thread x finish, thread a will be killed on exit.
#=> "axyz"
The following example illustrates the limit
parameter.
y = Thread.new { 4.times { sleep 0.1; puts 'tick... ' }}
puts "Waiting" until y.join(0.15)
This will produce:
tick...
Waiting
tick...
Waiting
tick...
tick...
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 |
# File 'thread.c', line 1135
static VALUE
thread_join_m(int argc, VALUE *argv, VALUE self)
{
VALUE limit;
rb_hrtime_t rel, *to = 0;
/*
* This supports INFINITY and negative values, so we can't use
* rb_time_interval right now...
*/
if (!rb_check_arity(argc, 0, 1) || NIL_P(argv[0])) {
/* unlimited */
}
else if (FIXNUM_P(limit = argv[0])) {
rel = rb_sec2hrtime(NUM2TIMET(limit));
to = &rel;
}
else {
to = double2hrtime(&rel, rb_num2dbl(limit));
}
return thread_join(rb_thread_ptr(self), to);
}
|
#key?(sym) ⇒ Boolean
3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 |
# File 'thread.c', line 3467
static VALUE
rb_thread_key_p(VALUE self, VALUE key)
{
ID id = rb_check_id(&key);
st_table *local_storage = rb_thread_ptr(self)->ec->local_storage;
if (!id || local_storage == NULL) {
return Qfalse;
}
else if (st_is_member(local_storage, id)) {
return Qtrue;
}
else {
return Qfalse;
}
}
|
#keys ⇒ Array
3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 |
# File 'thread.c', line 3511
static VALUE
rb_thread_keys(VALUE self)
{
st_table *local_storage = rb_thread_ptr(self)->ec->local_storage;
VALUE ary = rb_ary_new();
if (local_storage) {
st_foreach(local_storage, thread_keys_i, ary);
}
return ary;
}
|
#exit ⇒ Object #kill ⇒ Object #terminate ⇒ Object
Terminates thr
and schedules another thread to be run, returning the terminated Thread. If this is the main thread, or the last thread, exits the process.
2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 |
# File 'thread.c', line 2445
VALUE
rb_thread_kill(VALUE thread)
{
rb_thread_t *th = rb_thread_ptr(thread);
if (th->to_kill || th->status == THREAD_KILLED) {
return thread;
}
if (th == th->vm->main_thread) {
rb_exit(EXIT_SUCCESS);
}
thread_debug("rb_thread_kill: %p (%"PRI_THREAD_ID")\n", (void *)th, thread_id_str(th));
if (th == GET_THREAD()) {
/* kill myself immediately */
rb_threadptr_to_kill(th);
}
else {
threadptr_check_pending_interrupt_queue(th);
rb_threadptr_pending_interrupt_enque(th, eKillSignal);
rb_threadptr_interrupt(th);
}
return thread;
}
|
#name ⇒ String
show the name of the thread.
3127 3128 3129 3130 3131 |
# File 'thread.c', line 3127
static VALUE
rb_thread_getname(VALUE thread)
{
return rb_thread_ptr(thread)->name;
}
|
#name=(name) ⇒ String
set given name to the ruby thread. On some platform, it may set the name to pthread and/or kernel.
3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 |
# File 'thread.c', line 3141
static VALUE
rb_thread_setname(VALUE thread, VALUE name)
{
rb_thread_t *target_th = rb_thread_ptr(thread);
if (!NIL_P(name)) {
rb_encoding *enc;
StringValueCStr(name);
enc = rb_enc_get(name);
if (!rb_enc_asciicompat(enc)) {
rb_raise(rb_eArgError, "ASCII incompatible encoding (%s)",
rb_enc_name(enc));
}
name = rb_str_new_frozen(name);
}
target_th->name = name;
if (threadptr_initialized(target_th)) {
native_set_another_thread_name(target_th->thread_id, name);
}
return name;
}
|
#pending_interrupt?(error = nil) ⇒ Boolean
Returns whether or not the asynchronous queue is empty for the target thread.
If error
is given, then check only for error
type deferred events.
See ::pending_interrupt? for more information.
2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 |
# File 'thread.c', line 2076
static VALUE
rb_thread_pending_interrupt_p(int argc, VALUE *argv, VALUE target_thread)
{
rb_thread_t *target_th = rb_thread_ptr(target_thread);
if (!target_th->pending_interrupt_queue) {
return Qfalse;
}
if (rb_threadptr_pending_interrupt_empty_p(target_th)) {
return Qfalse;
}
if (rb_check_arity(argc, 0, 1)) {
VALUE err = argv[0];
if (!rb_obj_is_kind_of(err, rb_cModule)) {
rb_raise(rb_eTypeError, "class or module required for rescue clause");
}
if (rb_threadptr_pending_interrupt_include_p(target_th, err)) {
return Qtrue;
}
else {
return Qfalse;
}
}
else {
return Qtrue;
}
}
|
#priority ⇒ Integer
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.
Thread.current.priority #=> 0
3617 3618 3619 3620 3621 |
# File 'thread.c', line 3617
static VALUE
rb_thread_priority(VALUE thread)
{
return INT2NUM(rb_thread_ptr(thread)->priority);
}
|
#priority=(integer) ⇒ Object
Sets the priority of thr to integer. Higher-priority threads 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.
count1 = count2 = 0
a = Thread.new do
loop { count1 += 1 }
end
a.priority = -1
b = Thread.new do
loop { count2 += 1 }
end
b.priority = -2
sleep 1 #=> 1
count1 #=> 622504
count2 #=> 5832
3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 |
# File 'thread.c', line 3650
static VALUE
rb_thread_priority_set(VALUE thread, VALUE prio)
{
rb_thread_t *target_th = rb_thread_ptr(thread);
int priority;
#if USE_NATIVE_THREAD_PRIORITY
target_th->priority = NUM2INT(prio);
native_thread_apply_priority(th);
#else
priority = NUM2INT(prio);
if (priority > RUBY_THREAD_PRIORITY_MAX) {
priority = RUBY_THREAD_PRIORITY_MAX;
}
else if (priority < RUBY_THREAD_PRIORITY_MIN) {
priority = RUBY_THREAD_PRIORITY_MIN;
}
target_th->priority = (int8_t)priority;
#endif
return INT2NUM(target_th->priority);
}
|
#raise ⇒ Object #raise(string) ⇒ Object #raise(exception[, string [, array]]) ⇒ Object
Raises an exception from the given thread. The caller does not have to be thr
. See Kernel#raise for more information.
Thread.abort_on_exception = true
a = Thread.new { sleep(200) }
a.raise("Gotcha")
This will produce:
prog.rb:3: Gotcha (RuntimeError)
from prog.rb:2:in `initialize'
from prog.rb:2:in `new'
from prog.rb:2
2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 |
# File 'thread.c', line 2417
static VALUE
thread_raise_m(int argc, VALUE *argv, VALUE self)
{
rb_thread_t *target_th = rb_thread_ptr(self);
const rb_thread_t *current_th = GET_THREAD();
threadptr_check_pending_interrupt_queue(target_th);
rb_threadptr_raise(target_th, argc, argv);
/* To perform Thread.current.raise as Kernel.raise */
if (current_th == target_th) {
RUBY_VM_CHECK_INTS(target_th->ec);
}
return Qnil;
}
|
#report_on_exception ⇒ Boolean
Returns the status of the thread-local “report on exception” condition for this thr
.
The default value when creating a Thread is the value of the global flag Thread.report_on_exception.
See also #report_on_exception=.
There is also a class level method to set this for all new threads, see ::report_on_exception=.
2927 2928 2929 2930 2931 |
# File 'thread.c', line 2927
static VALUE
rb_thread_report_exc(VALUE thread)
{
return rb_thread_ptr(thread)->report_on_exception ? Qtrue : Qfalse;
}
|
#report_on_exception=(boolean) ⇒ Boolean
When set to true
, a message is printed on $stderr if an exception kills this thr
. See ::report_on_exception for details.
See also #report_on_exception.
There is also a class level method to set this for all new threads, see ::report_on_exception=.
2947 2948 2949 2950 2951 2952 |
# File 'thread.c', line 2947
static VALUE
rb_thread_report_exc_set(VALUE thread, VALUE val)
{
rb_thread_ptr(thread)->report_on_exception = RTEST(val);
return val;
}
|
#run ⇒ Object
2586 2587 2588 2589 2590 2591 2592 |
# File 'thread.c', line 2586
VALUE
rb_thread_run(VALUE thread)
{
rb_thread_wakeup(thread);
rb_thread_schedule();
return thread;
}
|
#safe_level ⇒ Integer
Returns the safe level.
This method is obsolete because $SAFE is a process global state. Simply check $SAFE.
3113 3114 3115 3116 3117 3118 |
# File 'thread.c', line 3113
static VALUE
rb_thread_safe_level(VALUE thread)
{
rb_warn("Thread#safe_level will be removed in Ruby 3.0");
return UINT2NUM(GET_VM()->safe_level_);
}
|
#set_trace_func(proc) ⇒ Proc #set_trace_func(nil) ⇒ nil
Establishes proc on thr as the handler for tracing, or disables tracing if the parameter is nil
.
See Kernel#set_trace_func.
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 |
# File 'vm_trace.c', line 569
static VALUE
thread_set_trace_func_m(VALUE target_thread, VALUE trace)
{
rb_execution_context_t *ec = GET_EC();
rb_thread_t *target_th = rb_thread_ptr(target_thread);
rb_threadptr_remove_event_hook(ec, target_th, call_trace_func, Qundef);
if (NIL_P(trace)) {
return Qnil;
}
else {
thread_add_trace_func(ec, target_th, trace);
return trace;
}
}
|
#status ⇒ String, ...
Returns the status of thr
.
"sleep"
-
Returned if this thread is sleeping or waiting on I/O
"run"
-
When this thread is executing
"aborting"
-
If this thread is aborting
false
-
When this thread is terminated normally
nil
-
If terminated with an exception.
a = Thread.new { raise("die now") } b = Thread.new { Thread.stop } c = Thread.new { Thread.exit } d = Thread.new { sleep } d.kill #=> #<Thread:0x401b3678 aborting> a.status #=> nil b.status #=> "sleep" c.status #=> false d.status #=> "aborting" Thread.current.status #=> "run"
See also the instance methods #alive? and #stop?
3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 |
# File 'thread.c', line 3027
static VALUE
rb_thread_status(VALUE thread)
{
rb_thread_t *target_th = rb_thread_ptr(thread);
if (rb_threadptr_dead(target_th)) {
if (!NIL_P(target_th->ec->errinfo) &&
!FIXNUM_P(target_th->ec->errinfo)) {
return Qnil;
}
else {
return Qfalse;
}
}
else {
return rb_str_new2(thread_status_name(target_th, FALSE));
}
}
|
#stop? ⇒ Boolean
3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 |
# File 'thread.c', line 3086
static VALUE
rb_thread_stop_p(VALUE thread)
{
rb_thread_t *th = rb_thread_ptr(thread);
if (rb_threadptr_dead(th)) {
return Qtrue;
}
else if (th->status == THREAD_STOPPED ||
th->status == THREAD_STOPPED_FOREVER) {
return Qtrue;
}
else {
return Qfalse;
}
}
|
#exit ⇒ Object #kill ⇒ Object #terminate ⇒ Object
Terminates thr
and schedules another thread to be run, returning the terminated Thread. If this is the main thread, or the last thread, exits the process.
2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 |
# File 'thread.c', line 2445
VALUE
rb_thread_kill(VALUE thread)
{
rb_thread_t *th = rb_thread_ptr(thread);
if (th->to_kill || th->status == THREAD_KILLED) {
return thread;
}
if (th == th->vm->main_thread) {
rb_exit(EXIT_SUCCESS);
}
thread_debug("rb_thread_kill: %p (%"PRI_THREAD_ID")\n", (void *)th, thread_id_str(th));
if (th == GET_THREAD()) {
/* kill myself immediately */
rb_threadptr_to_kill(th);
}
else {
threadptr_check_pending_interrupt_queue(th);
rb_threadptr_pending_interrupt_enque(th, eKillSignal);
rb_threadptr_interrupt(th);
}
return thread;
}
|
#thread_variable?(key) ⇒ Boolean
Returns true
if the given string (or symbol) exists as a thread-local variable.
me = Thread.current
me.thread_variable_set(:oliver, "a")
me.thread_variable?(:oliver) #=> true
me.thread_variable?(:stanley) #=> false
Note that these are not fiber local variables. Please see Thread#[] and Thread#thread_variable_get for more details.
3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 |
# File 'thread.c', line 3579
static VALUE
rb_thread_variable_p(VALUE thread, VALUE key)
{
VALUE locals;
ID id = rb_check_id(&key);
if (!id) return Qfalse;
if (LIKELY(!THREAD_LOCAL_STORAGE_INITIALISED_P(thread))) {
return Qfalse;
}
locals = rb_thread_local_storage(thread);
if (rb_hash_lookup(locals, ID2SYM(id)) != Qnil) {
return Qtrue;
}
else {
return Qfalse;
}
return Qfalse;
}
|
#thread_variable_get(key) ⇒ Object?
Returns the value of a thread local variable that has been set. Note that these are different than fiber local values. For fiber local values, please see Thread#[] and Thread#[]=.
Thread local values are carried along with threads, and do not respect fibers. For example:
Thread.new {
Thread.current.thread_variable_set("foo", "bar") # set a thread local
Thread.current["foo"] = "bar" # set a fiber local
Fiber.new {
Fiber.yield [
Thread.current.thread_variable_get("foo"), # get the thread local
Thread.current["foo"], # get the fiber local
]
}.resume
}.join.value # => ['bar', nil]
The value “bar” is returned for the thread local, where nil is returned for the fiber local. The fiber is executed in the same thread, so the thread local values are available.
3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 |
# File 'thread.c', line 3420
static VALUE
rb_thread_variable_get(VALUE thread, VALUE key)
{
VALUE locals;
if (LIKELY(!THREAD_LOCAL_STORAGE_INITIALISED_P(thread))) {
return Qnil;
}
locals = rb_thread_local_storage(thread);
return rb_hash_aref(locals, rb_to_symbol(key));
}
|
#thread_variable_set(key, value) ⇒ Object
Sets a thread local with key
to value
. Note that these are local to threads, and not to fibers. Please see Thread#thread_variable_get and Thread#[] for more information.
3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 |
# File 'thread.c', line 3441
static VALUE
rb_thread_variable_set(VALUE thread, VALUE id, VALUE val)
{
VALUE locals;
if (OBJ_FROZEN(thread)) {
rb_frozen_error_raise(thread, "can't modify frozen thread locals");
}
locals = rb_thread_local_storage(thread);
return rb_hash_aset(locals, rb_to_symbol(id), val);
}
|
#thread_variables ⇒ Array
Returns an array of the names of the thread-local variables (as Symbols).
thr = Thread.new do
Thread.current.thread_variable_set(:cat, 'meow')
Thread.current.thread_variable_set("dog", 'woof')
end
thr.join #=> #<Thread:0x401b3f10 dead>
thr.thread_variables #=> [:dog, :cat]
Note that these are not fiber local variables. Please see Thread#[] and Thread#thread_variable_get for more details.
3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 |
# File 'thread.c', line 3547
static VALUE
rb_thread_variables(VALUE thread)
{
VALUE locals;
VALUE ary;
ary = rb_ary_new();
if (LIKELY(!THREAD_LOCAL_STORAGE_INITIALISED_P(thread))) {
return ary;
}
locals = rb_thread_local_storage(thread);
rb_hash_foreach(locals, keys_i, ary);
return ary;
}
|
#to_s ⇒ String Also known as: inspect
Dump the name, id, and status of thr to a string.
3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 |
# File 'thread.c', line 3170
static VALUE
rb_thread_to_s(VALUE thread)
{
VALUE cname = rb_class_path(rb_obj_class(thread));
rb_thread_t *target_th = rb_thread_ptr(thread);
const char *status;
VALUE str, loc;
status = thread_status_name(target_th, TRUE);
str = rb_sprintf("#<%"PRIsVALUE":%p", cname, (void *)thread);
if (!NIL_P(target_th->name)) {
rb_str_catf(str, "@%"PRIsVALUE, target_th->name);
}
if ((loc = threadptr_invoke_proc_location(target_th)) != Qnil) {
rb_str_catf(str, " %"PRIsVALUE":%"PRIsVALUE,
RARRAY_AREF(loc, 0), RARRAY_AREF(loc, 1));
rb_gc_force_recycle(loc);
}
rb_str_catf(str, " %s>", status);
return str;
}
|
#value ⇒ Object
1173 1174 1175 1176 1177 1178 1179 |
# File 'thread.c', line 1173
static VALUE
thread_value(VALUE self)
{
rb_thread_t *th = rb_thread_ptr(self);
thread_join(th, 0);
return th->value;
}
|
#wakeup ⇒ Object
2539 2540 2541 2542 2543 2544 2545 2546 |
# File 'thread.c', line 2539
VALUE
rb_thread_wakeup(VALUE thread)
{
if (!RTEST(rb_thread_wakeup_alive(thread))) {
rb_raise(rb_eThreadError, "killed thread");
}
return thread;
}
|