www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to terminate thread under module destructor?

import core.thread.osthread : Thread;
import std.stdio : writeln;

__gshared static Thread th;
__gshared static size_t tht;

     void run()
     {
         writeln("run");
         while (tht == 0) {}
     }

     shared static this()
     {
         writeln("this");
         th = new Thread(&run).start();
     }

     shared static ~this()
     {
         writeln("~this");
         tht = 1;
     }

     void main()
     {
        writeln("main");
     }
Mar 09