Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ticket #596: thread_loop.d

File thread_loop.d, 1.3 kB (added by besquared, 1 year ago)

thread bashing code

Line 
1 import tango.core.Memory;
2 import tango.core.Thread;
3 import tango.stdc.stdio;
4
5
6 void threadFunc()
7 {
8     char[] msg1 = "fiber yield:  %.*s\n\0".dup;
9     char[] msg2 = "fiber resume: %.*s\n\0".dup;
10
11     void dg()
12     {
13         printf( msg1.ptr, Thread.getThis().name );
14         Fiber.yield();
15         printf( msg2.ptr, Thread.getThis().name );
16     }
17     Fiber fiber = new Fiber( &dg );
18     assert( fiber.state == Fiber.State.HOLD );
19
20     Thread.sleep( 0.3 );
21     fiber.call();
22     assert( fiber.state == Fiber.State.HOLD );
23     fiber.call();
24     assert( fiber.state == Fiber.State.TERM );
25     // allocate new thread to replace this one
26     printf( "recycling: %.*s\n", Thread.getThis().name );
27     Thread thread = new Thread( &threadFunc );
28     thread.name = Thread.getThis().name;
29     thread.start();
30 }
31
32
33 void main ()
34 {
35     Thread.getThis().name = "main".dup;
36     char[] n = "0".dup;
37     for( size_t i = 0; i < 10; ++i )
38     {
39         printf( "creating %.*s\n", n );
40         Thread thread = new Thread( &threadFunc );
41         thread.name = n;
42         thread.start();
43         ++n[0];
44         printf( "%p[%u]: %.*s\n", thread.name.ptr, thread.name.length, thread.name );
45     }
46
47     while( true )
48     {
49         printf( "collecting: %u\n", Thread.getAll().length );
50
51         GC.collect();
52
53         printf( "collected\n" );
54         Thread.sleep( 1.0 );
55     }
56 }