https://issues.dlang.org/show_bug.cgi?id=17127
Issue ID: 17127
Summary: bad example code for std.concurrency.Generator
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: minor
Priority: P1
Component: phobos
Assignee: [email protected]
Reporter: [email protected]
Found by Profile Anaysis who posted to D.learn:
http://forum.dlang.org/post/[email protected]
The example code for std.concurrency.Generator [1] throws an OwnerTerminated
exception. The code:
----
import std.concurrency;
import std.stdio;
void main()
{
auto tid = spawn(
{
while (true)
{
writeln(receiveOnly!int());
}
});
auto r = new Generator!int(
{
foreach (i; 1 .. 10)
yield(i);
});
foreach (e; r)
{
tid.send(e);
}
}
----
The exception gets thrown because the spawned thread still tries to receive
ints after the main thread has finished. The example code should be fixed.
[1] https://dlang.org/library/std/concurrency/generator.html
--