D Book page 402 Concurrency FAIL

thedeemon via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Feb 15 03:07:22 PST 2016


On Sunday, 14 February 2016 at 22:54:36 UTC, Brother Bill wrote:

> Please provide full replacement of this toy program that works 
> with D version 2.070.0

This one works fine for me in Windows with VisualD and DMD 
2.070.0:
------------------------------
import std.concurrency, std.stdio, std.exception;

void main() {
	auto low = 0,
		high = 100;
	auto tid = spawn(&writer);

	foreach (i; low .. high) {
		writeln("Main thread: ", i);
		tid.send(thisTid, i);
		enforce(receiveOnly!Tid() == tid);
	}
}

void writer() {
	scope(failure) return;
	for (;;) {
		auto msg = receiveOnly!(Tid, int)();
		writeln("secondary thread: ", msg[1]);
		msg[0].send(thisTid);
	}
}

------------------------------

Just one line added to writer() in order to catch OwnerTerminated 
exception and end the thread silently. Original version produced 
an error when main thread finished before the writer thread.


More information about the Digitalmars-d-learn mailing list