Class: ThreadGroup
Overview
ThreadGroup provides a means of keeping track of a number of threads as a
group.
A given Thread object can only belong to one ThreadGroup at a time; adding
a thread to a new group will remove it from any previous group.
Newly created threads belong to the same group as the thread from which they
were created.
Constant Summary collapse
- Default =
The default ThreadGroup created when Ruby starts; all Threads belong to it
by default. th->thgroup
Instance Method Summary collapse
-
#add(thread) ⇒ Object
Adds the given
threadto this group, removing it from any other group to which it may have previously been a member. -
#enclose ⇒ Object
Prevents threads from being added to or removed from the receiving ThreadGroup.
-
#enclosed? ⇒ Boolean
Returns
trueif thethgrpis enclosed. -
#list ⇒ Array
Returns an array of all existing Thread objects that belong to this group.
Instance Method Details
#add(thread) ⇒ Object
Adds the given thread to this group, removing it from any other group to which it may have previously been a member.
puts "Initial group is #{ThreadGroup::Default.list}"
tg = ThreadGroup.new
t1 = Thread.new { sleep }
t2 = Thread.new { sleep }
puts "t1 is #{t1}"
puts "t2 is #{t2}"
tg.add(t1)
puts "Initial group now #{ThreadGroup::Default.list}"
puts "tg group now #{tg.list}"
This will produce:
Initial group is #<Thread:0x401bdf4c>
t1 is #<Thread:0x401b3c90>
t2 is #<Thread:0x401b3c18>
Initial group now #<Thread:0x401b3c18>#<Thread:0x401bdf4c>
tg group now #<Thread:0x401b3c90>
4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 |
# File 'thread.c', line 4705 static VALUE thgroup_add(VALUE group, VALUE thread) { rb_thread_t *target_th = rb_thread_ptr(thread); struct thgroup *data; if (OBJ_FROZEN(group)) { rb_raise(rb_eThreadError, "can't move to the frozen thread group"); } TypedData_Get_Struct(group, struct thgroup, &thgroup_data_type, data); if (data->enclosed) { rb_raise(rb_eThreadError, "can't move to the enclosed thread group"); } if (!target_th->thgroup) { return Qnil; } if (OBJ_FROZEN(target_th->thgroup)) { rb_raise(rb_eThreadError, "can't move from the frozen thread group"); } TypedData_Get_Struct(target_th->thgroup, struct thgroup, &thgroup_data_type, data); if (data->enclosed) { rb_raise(rb_eThreadError, "can't move from the enclosed thread group"); } target_th->thgroup = group; return group; } |
#enclose ⇒ Object
Prevents threads from being added to or removed from the receiving ThreadGroup.
New threads can still be started in an enclosed ThreadGroup.
ThreadGroup::Default.enclose #=> #<ThreadGroup:0x4029d914>
thr = Thread.new { Thread.stop } #=> #<Thread:0x402a7210 sleep>
tg = ThreadGroup.new #=> #<ThreadGroup:0x402752d4>
tg.add thr
#=> ThreadError: can't move from the enclosed thread group
4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 |
# File 'thread.c', line 4648 static VALUE thgroup_enclose(VALUE group) { struct thgroup *data; TypedData_Get_Struct(group, struct thgroup, &thgroup_data_type, data); data->enclosed = 1; return group; } |
#enclosed? ⇒ Boolean
Returns true if the thgrp is enclosed. See also ThreadGroup#enclose.
4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 |
# File 'thread.c', line 4667 static VALUE thgroup_enclosed_p(VALUE group) { struct thgroup *data; TypedData_Get_Struct(group, struct thgroup, &thgroup_data_type, data); if (data->enclosed) return Qtrue; return Qfalse; } |
#list ⇒ Array
Returns an array of all existing Thread objects that belong to this group.
ThreadGroup::Default.list #=> [#<Thread:0x401bdf4c run>]
4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 |
# File 'thread.c', line 4616 static VALUE thgroup_list(VALUE group) { VALUE ary = rb_ary_new(); rb_vm_t *vm = GET_THREAD()->vm; rb_thread_t *th = 0; list_for_each(&vm->living_threads, th, vmlt_node) { if (th->thgroup == group) { rb_ary_push(ary, th->self); } } return ary; } |