Thread overview
Thread will get garbage collected?
Jan 16, 2017
JN
Jan 16, 2017
kinke
Jan 17, 2017
thedeemon
Jan 17, 2017
ketmar
Jan 17, 2017
ketmar
Jan 17, 2017
Rene Zwanenburg
January 16, 2017
I'm looking at the example code for core.thread Thread class:

new Thread({
    // Codes to run in the newly created thread.
}).start();


let's imagine I put the code in a function:

void loadFileAsync(string path)
{
  new Thread({
      writeln(readText(path));            // imagine the file is big so it takes some time
  }).start();
}

void main()
{
  loadFileAsync("foo.txt");
}

Am I correctly understanding, that after going out of scope, it's possible for GC to destroy my thread before the file finishes loading? How to prevent GC from destroying my thread before it finishes and make sure the file is loaded completely?
January 16, 2017
On Monday, 16 January 2017 at 22:08:56 UTC, JN wrote:
> Am I correctly understanding, that after going out of scope, it's possible for GC to destroy my thread before the file finishes loading? How to prevent GC from destroying my thread before it finishes and make sure the file is loaded completely?

The GC may collect it as soon as there's no reference to the Thread object anymore. But you'll probably want to keep a reference anyway, to join it (wait until it's finished), something like:

Thread loadFileAsync(string path)
{
  // start() returns `this`
  return new Thread({ writeln(readText(path)); }).start();
}

void main()
{
  auto thread = loadFileAsync("foo.txt");

  // [do something useful in parallel]

  // wait for other thread
  thread.join();
}

January 17, 2017
On Monday, 16 January 2017 at 22:08:56 UTC, JN wrote:

> Am I correctly understanding, that after going out of scope, it's possible for GC to destroy my thread before the file finishes loading? How to prevent GC from destroying my thread before it finishes and make sure the file is loaded completely?

As far as I know, GC never kills threads.
January 17, 2017
On Tuesday, 17 January 2017 at 07:53:32 UTC, thedeemon wrote:
> On Monday, 16 January 2017 at 22:08:56 UTC, JN wrote:
>
>> Am I correctly understanding, that after going out of scope, it's possible for GC to destroy my thread before the file finishes loading? How to prevent GC from destroying my thread before it finishes and make sure the file is loaded completely?
>
> As far as I know, GC never kills threads.


yes. it doesn't matter if a thread is achored or not, it won't be killed. it is very easy to check, actually:

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.
January 17, 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?
January 17, 2017
On Tuesday, 17 January 2017 at 08:58:33 UTC, Arun Chandrasekaran wrote:
> Interesting. Why doesn't the thread get GC'd in this case even without any reference still active?

basically 'cause there is no reliable way to correctly "abort" a thread. nobody knows what is really going on inside it, so shooting it down is very unsafe. i.e. aborting thread in the middle of something is very-very wrong, so it is programmer's responsibility to stop (or detach) all the created threads.

note that fibers *will* be collected.
January 17, 2017
On Tuesday, 17 January 2017 at 08:58:33 UTC, Arun Chandrasekaran wrote:
> Interesting. Why doesn't the thread get GC'd in this case even without any reference still active?

There will be a reference to it in druntime itself:
http://dlang.org/phobos/core_thread.html#.Thread.getThis