# Should initialize the group of threads

**URL:** <https://www.ruby-forum.com/t/should-initialize-the-group-of-threads/126469>\
**Category:** Development\
**Created:** [January 18, 2008, 3:49pm UTC](https://www.ruby-forum.com/t/should-initialize-the-group-of-threads/126469 "2008-01-18T15:49:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sheepman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@sheepman](https://www.ruby-forum.com/u/sheepman)\
**Post date:** [January 18, 2008, 3:49pm UTC](https://www.ruby-forum.com/t/should-initialize-the-group-of-threads/126469/1 "2008-01-18T15:49:30Z")

</div>

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);

---

<div class="post-metadata">

**Author:** ![sheepman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@sheepman](https://www.ruby-forum.com/u/sheepman)\
**Post date:** [January 18, 2008, 4:57pm UTC](https://www.ruby-forum.com/t/should-initialize-the-group-of-threads/126469/2 "2008-01-18T16:57:02Z")

</div>

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-\>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
