This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -1,32 +1,20 @@
import tools.threads, std.stdio, std.stream, std.thread;
import std.algorithm, std.concurrency, std.stdio;
void main() {
// line or EOF
struct InputLine {
string data;
bool eof;
static InputLine opCall(string s) { InputLine res; res.data = s; return res; }
static InputLine EOF() { InputLine res; res.eof = true; return res; }
}
auto LineCh = new MessageChannel!(InputLine),
ResultCh = new MessageChannel!(int);
auto printer = new Thread({
int count;
while (true) {
auto line = LineCh.get();
if (line.eof) break;
count ++;
writefln(count, ": ", line.data);
}
ResultCh.put(count);
return 0;
});
printer.start;
auto file = new File("input.txt");
while (!file.eof()) {
auto line = file.readLine();
LineCh.put(InputLine(line));
}
LineCh.put(InputLine.EOF());
writefln("Count: ", ResultCh.get());
auto printer = spawn(&printTask, thisTid);
auto f = File("input.txt","r");
foreach (string line; lines(f))
send(printer, line);
send(printer, true); //EOF
auto n = receiveOnly!(int)();
stdout.writefln("\n%d lines printed.", n);
}
void printTask(Tid reader) {
int n = 0;
for (bool eof = false; !eof;)
receive(
(string line) {stdout.write(line); n++;},
(bool) {send(reader, n); eof = true;}
);
}