You could tell your thread via a shared variable that main has ended:

import std.stdio;
import std.concurrency;
import core.thread;

shared bool end = false;

void thread ()
{
        for (;;)
        {
                Thread.sleep (500.msecs);
                synchronized
                {
                        if (end) break;
                }
        }
        writeln ("thread ends");
}

void main ()
{
        spawn (&thread);
        Thread.sleep (3.seconds);
        writeln ("main ends");
        synchronized
        {
                end = true;
        }
}

or you could use the fact, that receiveTimeout throws an exception, when main ends (although I don't know if this is the intended behaviour; the manual just says that it throws an exception when the sending thread was terminated):

import std.stdio;
import std.concurrency;
import core.thread;

void thread ()
{
        for (;;)
        {
                try
                {
                        receiveTimeout (500.msecs);
                }
                catch (Throwable)
                {
                        break;
                }

        }
        writeln ("thread ends");
}

void main ()
{
        auto tid = spawn (&thread);
        Thread.sleep (3.seconds);
        writeln ("main ends");
}


Reply via email to