D Language 2.0

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon Jan 18 09:23:20 PST 2010


Andrei Alexandrescu wrote:
> Nick Sabalausky wrote:
> [snip]
>> It's been no worse at threading than C/C++ for quite some time. It's 
>> just starting to have a threading model that kicks the crap out of the 
>> threading in the vast majority of languages out there.
> 
> BTW, that effort is going quite well. For example, a producer-consumer 
> file copy program using the proposed API has 20 lines, correctness and all.
> 
> import std.algorithm, std.concurrency, std.stdio;
> 
> void main() {
>    enum bufferSize = 1024 * 100;
>    auto tid = spawn(&writer);
>    // Read loop
>    auto src = stdin.by!(ubyte)();
>    for (;;) {
>       auto buffer = UniqueArray!(ubyte)(bufferSize);
>       auto length = copy(take(src, bufferSize), buffer).length;
>       send(tid, move(buffer));
>       if (length == 0) break;
>    }
> }
> 
> void writer() {
>    // Write loop
>    auto tgt = stdout.by!(ubyte)();
>    for (;;) {
>       auto buffer = receiveOnly!(UniqueArray!ubyte)();
>       copy(buffer, tgt);
>    }
> }
> 
> 
> Andrei

Sorry for the monologue. Actually I reworked the example into the even 
simpler:

import std.concurrency, std.stdio;

void main() {
    enum bufferSize = 1024 * 100;
    auto tid = spawn(&writer);
    // Read loop
    foreach (immutable(ubyte)[] buffer; stdin.byChunk(bufferSize)) {
       send(tid, buffer);
    }
}

void writer() {
    // Write loop
    for (;;) {
       auto buffer = receiveOnly!(immutable(ubyte)[])();
       tgt.rawWrite(buffer);
    }
}

We actually have implemented all the pieces to make this work.


Andrei



More information about the Digitalmars-d mailing list