Newbie problem
Roger Stokes
rs at rogerstokes.free-online.co.uk
Wed Jun 19 12:14:56 PDT 2013
Many thanks for swift response. I'm still in difficulty, alas.
There were two
possibilities suggested: firstly,
> Adam D. Ruppe wrote:
> The problem here is that byChunk returns a mutable buffer, type
> ubyte[]. If you said "foreach(ubyte[] buffer;
> stdin.byChunk(bufferSize)) {...}" you should be able to compile
> it.
>
I tried this, which I hope is correct:
---------------------------
void main() {
enum bufferSize = 1024 * 100;
auto tid = spawn(&fileWriter);
// Read loop
foreach (ubyte[] buffer; stdin.byChunk(bufferSize)) {
send(tid, buffer);
}
}
void fileWriter() {
// Write loop
for (;;) {
auto buffer = receiveOnly!(immutable(ubyte)[])();
stdout.write(buffer);
}
}
------------------------------------------------------------
and got this compiler diagnostic:
c:\D\dmd2\windows\bin\..\..\src\phobos\std\concurrency.d(571):
Error: static assert "Aliases to mutable thread-local data not
allowed."
page406x.d(12): instantiated from here: send!(ubyte[])
For the second possibility, ...
> Since you aren't keeping a copy of the buffer, just changing
> the type should be enough for your program to work.
>
> If you needed an immutable copy, if you were going to store it
> or something, the way you'd do that is to call buffer.idup:
>
> foreach(ubyte[] temporaryBuffer; stdin.byChunk(bufferSize) {
> immutable(ubyte)[] permanentBuffer = temporaryBuffer.idup;
> // you can now use the permanentBuffer
> }
I tried this ---------------------------------------------------
import std.algorithm, std.concurrency, std.stdio;
void main() {
enum bufferSize = 1024 * 100;
auto tid = spawn(&fileWriter);
// Read loop
foreach(ubyte[] temporaryBuffer; stdin.byChunk(bufferSize)) {
immutable(ubyte)[] permanentBuffer = temporaryBuffer.idup;
// you can now use the permanentBuffer
send(tid, permanentBuffer);
}
}
void fileWriter() {
// Write loop
for (;;) {
auto buffer = receiveOnly!(immutable(ubyte)[])();
stdout.write(buffer);
}
}
--------------------------------
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
----------------
0x004068EA
0x004081B5
0x00407AB4
0x00407C6A
0x00407CA0
0x00407D1C
0x00407883
0x004068AB
0x004020E9
0x00414D51
0x00434CB4
0x76E51603 in RtlInitializeExceptionChain
0x76E515D6 in RtlInitializeExceptionChain
----------------
[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!
Any further help would be much appreciated.
Regards,
Roger Stokes
More information about the Digitalmars-d-learn
mailing list