How to send ownerTid into a parallel foreach loop?
adnan338
relay.public.adnan at outlook.com
Sat Jun 27 05:19:16 UTC 2020
I have a list of files to download, and I want to download them
in parallel. At the end of each of those parallel download I want
to send the main thread a message from the parallel loop.
import std.concurrency, std.parallelism;
string[] files = ["a", "b", "c"];
void download(string[] links)
{
auto owner = ownerTid();
foreach (link; links.parallel())
{
// something
owner.send(false);
}
owner.send(true);
}
void main()
{
// I do not want my main thread to freeze
spawn(&download, files);
bool done = false;
while (!done)
receive((bool isDone) { done = isDone; });
}
But the compiler says:
Error: static assert: "Aliases to mutable thread-local data not
allowed."
source/app.d(19,7): instantiated from here: spawn!(void
function(string[]), string[])
How should I go around this? I think the parallel block is being
restricted from sending messages to the owner.
Here,
More information about the Digitalmars-d-learn
mailing list