Threads and OwnerTerminated

Andrej Mitrovic andrej.mitrovich at gmail.com
Thu Nov 17 13:09:00 PST 2011


Ah, I should have read the following parts where it describes thread
termination, in TDPL. Woops.

On 11/17/11, Andrej Mitrovic <andrej.mitrovich at gmail.com> wrote:
> 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