Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
class method
Thread.new
(*untyped) { (?) -> void } -> ::Thread
Thread.new { ... } → thread
Thread.new(*args, &proc) → thread
Thread.new(*args) { |args| ... } → thread
thread.c
View on GitHub ↗
static VALUE
thread_s_new(int argc, VALUE *argv, VALUE klass)
{
rb_thread_t *th;
VALUE thread = rb_thread_alloc(klass);
if (GET_RACTOR()->threads.main->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;
}
Creates a new thread executing the given block.
Any args given to ::new will be passed to the block:
Example
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.