www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Thread will get garbage collected?

reply JN <666total wp.pl> writes:
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?
Jan 16
next sibling parent kinke <noone nowhere.com> writes:
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(); }
Jan 16
prev sibling parent reply thedeemon <dlang thedeemon.com> writes:
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.
Jan 16
parent reply ketmar <ketmar ketmar.no-ip.org> writes:
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.
Jan 17
parent reply Arun Chandrasekaran <aruncxy gmail.com> writes:
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?
Jan 17
next sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
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.
Jan 17
prev sibling parent Rene Zwanenburg <renezwanenburg gmail.com> writes:
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
Jan 17