Retrieve the data of all the threads together once all threads are completed in a task pool
vino via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Aug 26 11:26:30 PDT 2017
On Saturday, 26 August 2017 at 17:38:37 UTC, Vino.B wrote:
> Hi,
>
> Can some one provide me an example of how to wait for all the
> threads to be completed in a taskPool and then retrieve the
> data of all the threads together instead of getting the data of
> each threads(after successfully executed). For example, the
> below test program outputs only one string "Welcome" but not
> the string "Home".
>
> import std.stdio;
> import std.parallelism;
>
> string Data;
> auto Textarr = [ "Welcome", "Home" ];
>
> string fn (string text)
> { return text; }
>
> string Submain ()
> {
> foreach ( i; taskPool.parallel(Textarr[0 .. $], 1))
> {
> auto Task = task(&fn, i);
> Task.executeInNewThread();
> auto TaskData = Task.workForce;
> Data ~= TaskData;
> }
> return Data;
> }
>
> void main ()
> {
> Submain;
> writeln(Data[0 .. $]);
> }
>
> From,
> Vino.B
Hi All,
Was able to find a solution, but the output writes additional
empty lines., request your help on how to print without the empty
lines.
Program:
import std.stdio;
import std.parallelism;
import std.algorithm;
import std.string;
string Data;
auto Textarr = [ "Welcome", "Home" ];
string endresult;
string fn (string text)
{ return text; }
void main ()
{
string text;
auto endresult = taskPool.workerLocalStorage(text);
foreach ( i; parallel(Textarr[0 .. $], 1))
{
auto Task = task(&fn, i);
Task.executeInNewThread();
auto TaskData = Task.workForce;
endresult.get ~= TaskData;
}
foreach (i; sums.toRange)
{ writeln(i); }
}
Output:
C:\Users\admin\Desktop\Script>rdmd test.d
Welcome
Home
C:\Users\admin\Desktop\Script>
More information about the Digitalmars-d-learn
mailing list