Should initialize the group of threads

e$B$3$s$P$s$Oe(B sheepman e$B$G$9!#e(B

2e$B$A$c$s$M$k$N%9%l$G8+$+$1$?%P%0$G$9!#e(B
ThreadGroup#add e$B$,$&$^$/F0$-$^$;$s!#e(B

$ cat t.rb
g = ThreadGroup.new
t = Thread.new{sleep 5}
z=0
begin
g.add t
p g.list
z+=1
Thread.pass
end until g.list.include?(t)
p z

$ ruby-1.9 -v t.rb
ruby 1.9.0 (2008-01-18 revision 0) [i686-linux]
[]
[]
[]
[#<Thread:0x405754c8 sleep>]
4

e$B0J2<$O%Q%C%A$G$9!#e(B

$ svn diff thread.c
Index: thread.c

— thread.c (revision 15114)
+++ thread.c (working copy)
@@ -383,7 +383,8 @@ thread_create_core(VALUE thval, VALUE ar
th->first_func = fn;

 th->priority = GET_THREAD()->priority;
  • th->thgroup = GET_THREAD()->thgroup;
  • native_mutex_initialize(&th->interrupt_lock);
    /* kick thread */
    st_insert(th->vm->living_threads, thval, (st_data_t)
    th->thread_id);

e$B$3$s$P$s$Oe(B sheepman e$B$G$9!#e(B
e$B%U%j!<%:$7$?e(B ThreadGroup e$B$K=jB0$9$k%9%l%C%I$+$i$Oe(B
e$B%9%l%C%I$r@8@.$G$-$J$$$H$$$&;EMM$,$"$k$3$H$r;W$$=P$7$^$7$?!#e(B
e$B$3$N>l=j$G%A%’%C%/$9$Y$-$+$OJ,$+$j$^$;$s$,%Q%C%A$H%F%9%H$G$9!#e(B

Index: thread.c

— thread.c (revision 15114)
+++ thread.c (working copy)
@@ -375,15 +375,20 @@ thread_create_core(VALUE thval, VALUE ar
{
rb_thread_t *th;

  • GetThreadPtr(thval, th);
  • if (OBJ_FROZEN(GET_THREAD()->thgroup)) {

  •  rb_raise(rb_eThreadError,
    
  •           "can't start a new thread (frozen ThreadGroup)");
    
  • }

  • GetThreadPtr(thval, th);
    /* setup thread environment */
    th->first_args = args;
    th->first_proc = fn ? Qfalse : rb_block_proc();
    th->first_func = fn;

    th->priority = GET_THREAD()->priority;

  • th->thgroup = GET_THREAD()->thgroup;
  • native_mutex_initialize(&th->interrupt_lock);
    /* kick thread */
    st_insert(th->vm->living_threads, thval, (st_data_t)
    th->thread_id);
    Index: test/ruby/test_thread.rb
    ===================================================================
    — test/ruby/test_thread.rb (revision 15114)
    +++ test/ruby/test_thread.rb (working copy)
    @@ -22,3 +22,36 @@ class TestThread < Test::Unit::TestCase
    end
    end

+class TestThreadGroup < Test::Unit::TestCase

  • def test_thread_init
  • thgrp = ThreadGroup.new
  • Thread.new{
  •  thgrp.add(Thread.current)
    
  •  assert_equal(thgrp, Thread.new{sleep 1}.group)
    
  • }.join
  • end
  • def test_frozen_thgroup
  • thgrp = ThreadGroup.new
  • Thread.new{
  •  thgrp.add(Thread.current)
    
  •  thgrp.freeze
    
  •  assert_raise(ThreadError) do
    
  •    Thread.new{1}.join
    
  •  end
    
  • }.join
  • end
  • def test_enclosed_thgroup
  • thgrp = ThreadGroup.new
  • thgrp.enclose
  • Thread.new{
  •  assert_raise(ThreadError) do
    
  •    thgrp.add(Thread.current)
    
  •  end
    
  •  assert_nothing_raised do
    
  •    Thread.new{1}.join
    
  •  end
    
  • }.join
  • end
    +end