www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 17127] New: bad example code for std.concurrency.Generator

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

--
Jan 30 2017