Thread will get garbage collected?

JN via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jan 16 14:08:56 PST 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?


More information about the Digitalmars-d-learn mailing list