Thread will get garbage collected?

Arun Chandrasekaran via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jan 17 00:58:33 PST 2017


On Tuesday, 17 January 2017 at 08:12:50 UTC, ketmar wrote:
> import core.thread;
> import core.time;
> import std.stdio;
>
> void threadStarter (string path) {
>   new Thread({
>     for (;;) {
>       writeln(path);
>       Thread.sleep(1.seconds);
>     }
>   }).start();
> }
>
>
> class A {
>   ~this () { import core.stdc.stdio; printf("i'm dead now!\n"); 
> }
> }
>
>
> void main () {
>   threadStarter("foo.txt");
>   auto a = new A();
>   import core.memory : GC;
>   for (;;) {
>     writeln("collect...");
>     GC.collect();
>     Thread.sleep(500.msecs);
>     a = null;
>   }
> }
>
>
> one will eventually see "i'm dead now!", yet "foo.txt" will 
> continue to appear.

Interesting. Why doesn't the thread get GC'd in this case even 
without any reference still active?


More information about the Digitalmars-d-learn mailing list