[Issue 17020] New: std.parallelism.taskpool amap should accept lambdas

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu Dec 22 17:52:04 PST 2016


https://issues.dlang.org/show_bug.cgi?id=17020

          Issue ID: 17020
           Summary: std.parallelism.taskpool amap should accept lambdas
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: phobos
          Assignee: nobody at puremagic.com
          Reporter: greeenify at gmail.com

void main()
{
    auto w = iota(0,1_000_000);
    int[] foo;

    // Not OK, dmd can't infer lambda isn't a delegate
    // foo = taskPool().amap!(a => a + 1)(w);

    // OK:
    foo = taskPool().amap!`a+1`(w); // string lambdas, yeah!
    foo = taskPool().amap!(function int(int a) => a + 1)(w);
    static int func(int a) { return a + 1; }
    foo = taskPool().amap!func(w);
}


In the forum thread a quick & dirty solution was posted:


private auto pmap(alias fun, R)(R range) if(isInputRange!R) {
    import std.parallelism;
    import core.sync.mutex;

    static __gshared Mutex mutex;
    if(mutex is null) mutex = new Mutex;
    typeof(fun(range.front))[] values;
    foreach(i, value; range.parallel) {
        auto newValue = fun(value);
        synchronized(mutex) {
            if(values.length < i + 1) values.length = i + 1;
            values[i] = newValue;
        }
    }

    return values;
}


http://forum.dlang.org/post/zkyhyjtyjdyshxyqoryk@forum.dlang.org

--


More information about the Digitalmars-d-bugs mailing list