On 7/12/22 11:47, Bagomot wrote: > I now have a couple more questions about `Task`: > 1) How to run a non-static class method through `task`? > 2) How to use `taskPool` to run a series of tasks of the same type (from > question 1)?
As a friendly reminder, these questions could be more useful in a separate forum thread. :)
import std.stdio; import std.parallelism; import std.algorithm; import std.range; interface Animal { string song(); } class Dog : Animal { string voice_; this(string voice) { this.voice_ = voice; } string song() { return voice_ ~ " " ~ voice_; } } void main() { auto voices = [ "hav", "woof", "bark", "gav", "grrr" ]; auto dogs = voices.map!(voice => new Dog(voice)).array; // No need to specify this; just being silly... const workerCount = totalCPUs + 7; auto tp = new TaskPool(workerCount); scope (exit) tp.finish(); // a) Classic foreach loop foreach (dog; tp.parallel(dogs)) { writeln(dog.song); } // b) Adding individual tasks (could be a foreach loop) dogs.each!(dog => tp.put(task!(d => writeln(d.song))(dog))); } Ali