digitalmars.D.learn - Thread will get garbage collected?
- JN (20/20) Jan 16 2017 I'm looking at the example code for core.thread Thread class:
- kinke (17/21) Jan 16 2017 The GC may collect it as soon as there's no reference to the
- thedeemon (2/6) Jan 16 2017 As far as I know, GC never kills threads.
- ketmar (30/36) Jan 17 2017 yes. it doesn't matter if a thread is achored or not, it won't be
- Arun Chandrasekaran (3/31) Jan 17 2017 Interesting. Why doesn't the thread get GC'd in this case even
- ketmar (8/10) Jan 17 2017 basically 'cause there is no reliable way to correctly "abort" a
- Rene Zwanenburg (4/6) Jan 17 2017 There will be a reference to it in druntime itself:
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 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(); }
Jan 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?As far as I know, GC never kills threads.
Jan 16 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: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.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 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?
Jan 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.
Jan 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:
Jan 17 2017