класс ThreadGroup
ThreadGroup предоставляет способ отслеживания группы потоков.
Объект Thread может принадлежать только одному ThreadGroup за раз; добавление потока в новую группу удалит его из любой предыдущей группы.
Новосозданные потоки принадлежат той же группе, что и поток, из которого они были созданы.
Константы
- Default
-
По умолчанию создаваемая группа потоков ThreadGroup при запуске Ruby; все потоки по умолчанию принадлежат ей.
Общедоступные методы экземпляров
static VALUE
thgroup_add(VALUE group, VALUE thread)
{
rb_thread_t *th;
struct thgroup *data;
GetThreadPtr(thread, th);
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 (!th->thgroup) {
return Qnil;
}
if (OBJ_FROZEN(th->thgroup)) {
rb_raise(rb_eThreadError, "can't move from the frozen thread group");
}
TypedData_Get_Struct(th->thgroup, struct thgroup, &thgroup_data_type, data);
if (data->enclosed) {
rb_raise(rb_eThreadError,
"can't move from the enclosed thread group");
}
th->thgroup = group;
return group;
} Добавляет указанный thread в эту группу, удаляя его из любой другой группы, членом которой он мог быть ранее.
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}"
Это приведет к:
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>
static VALUE
thgroup_enclose(VALUE group)
{
struct thgroup *data;
TypedData_Get_Struct(group, struct thgroup, &thgroup_data_type, data);
data->enclosed = 1;
return group;
} Запрещает добавление или удаление потоков из принимающей группы потоков ThreadGroup.
Новые потоки все еще могут быть запущены в закрытой группе потоков 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
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;
} Возвращает true, если группа потоков thgrp закрыта. См. также #enclose.
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;
} Возвращает массив всех существующих объектов потоков Thread, которые принадлежат этой группе.
ThreadGroup::Default.list #=> [#<Thread:0x401bdf4c run>]
Ruby Core © 1993–2017 Yukihiro Matsumoto
Licensed under the Ruby License.
Ruby Standard Library © contributors
Licensed under their own licenses.