Newbie problem

Ali Çehreli acehreli at yahoo.com
Wed Jun 19 12:50:53 PDT 2013


On 06/19/2013 12:14 PM, Roger Stokes wrote:

 > and the result compiled and executed with no error signals, but the
 > resulting output of the file-copy was not the same as the input, it
 > looked like this:
 >
 > std.concurrency.OwnerTerminated at std\concurrency.d(248): Owner terminated

For a clean exit, the owner must tell the worker that there is no more 
data (see struct Done below). It must then wait for it to exit (see 
core.thread.thread_joinAll below):

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

struct Done
{}

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

    tid.send(Done());

    import core.thread;
    thread_joinAll();
}

void fileWriter() {
    // Write loop
     bool done = false;

     while (!done) {
        receive(
            (immutable(ubyte)[] buffer) {
                stdout.write(buffer);
            },

            (Done _) {
                done = true;
            });
    }
}


 > ----------------
 > [47, 47, 32, 101, 120, 97, ....
 >
 > ....
 > The numbers 47, 47, 32, etc look like the ASCII indexes of the characters
 > which should be in the output,  not the characters themselves!

You are writing ubyte[]. What you see is the default output for such a 
slice. If you are sure that the data is UTF-8, then you can cast before 
outputting:

                stdout.write(cast(string)buffer);

Ali



More information about the Digitalmars-d-learn mailing list