[dmd-concurrency] draft 4

Andrei Alexandrescu andrei at erdani.com
Tue Jan 12 00:45:35 PST 2010


To be found at the usual location:

http://erdani.com/d/fragment.preview.pdf

I didn't add a lot of text this time around but I do have a full example 
of communicating threads. Skip to the last section for explanation. I 
paste the code below. I think it looks pretty darn cool. Sean, please 
let me know if it floats your boat.

import std.concurrency, std.stdio;

void main() {
    auto low = 0, high = 1000;
    auto tid = spawn(&fun);
    foreach (i; low .. high) {
       writeln("Main thread: ", message, i);
       tid.send(thisTid, i);
       enforce(receiveOnly!Tid() == tid);
    }
    // Signal the other thread
    tid.send(Tid(), 0);
}

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


Andrei


More information about the dmd-concurrency mailing list