Threads and OwnerTerminated

Andrej Mitrovic andrej.mitrovich at gmail.com
Thu Nov 17 11:56:25 PST 2011


This is an example from TDPL:

import std.concurrency;
import std.exception;
import std.stdio;

void main()
{
    auto low = 0;
    auto high = 100;
    auto tid = spawn(&writer);

    foreach (i; low .. high)
    {
        writeln("Main thread: ", i);
        tid.send(thisTid, i);
        enforce(receiveOnly!Tid() == tid);
    }
}

void writer()
{
    for (;;)
    {
        auto msg = receiveOnly!(Tid, int)();
        writeln("Secondary thread: ", msg[1]);
        msg[0].send(thisTid);
    }
}

This will throw an OwnerTerminated exception on exit since the writer
thread calls receiveOnly() after the main thread was killed:
std.concurrency.OwnerTerminated at std\concurrency.d(214): Owner terminated

So what is expected here, am I always supposed to wrap receive() in a try/catch?

Btw, is std.concurrency planned to be expanded/improved upon? Or is
this a low-level portable messaging API (emphasis on messaging,
core.thread is lower-level), on top of which more complex APIs can be
built on?


More information about the Digitalmars-d-learn mailing list