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();
}

Reply via email to