[Issue 17127] New: bad example code for std.concurrency.Generator

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Mon Jan 30 03:17:07 PST 2017


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: nobody at puremagic.com
          Reporter: ag0aep6g at gmail.com

Found by Profile Anaysis who posted to D.learn:
http://forum.dlang.org/post/vlvqmzrfvqmxzsfxpslu@forum.dlang.org

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

--


More information about the Digitalmars-d-bugs mailing list