www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to send ownerTid into a parallel foreach loop?

reply adnan338 <relay.public.adnan outlook.com> writes:
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,
Jun 26 2020
parent reply Kagamin <spam here.lot> writes:
std.concurrency is for noninteractive appications, the approach 
with gui timer was the correct one.
Jun 27 2020
parent reply adnan338 <relay.public.adnan outlook.com> writes:
On Saturday, 27 June 2020 at 07:31:56 UTC, Kagamin wrote:
 std.concurrency is for noninteractive appications, the approach 
 with gui timer was the correct one.
Thank you. That works but my progress bar is sometimes getting stuck because of a possible data race. See https://forum.dlang.org/post/gacweulvbyorksetidcj forum.dlang.org Today I discovered Glib Idle so I was trying that out.
Jun 27 2020
parent Kagamin <spam here.lot> writes:
On Saturday, 27 June 2020 at 07:51:21 UTC, adnan338 wrote:
 On Saturday, 27 June 2020 at 07:31:56 UTC, Kagamin wrote:
 std.concurrency is for noninteractive appications, the 
 approach with gui timer was the correct one.
Thank you. That works but my progress bar is sometimes getting stuck because of a possible data race. See https://forum.dlang.org/post/gacweulvbyorksetidcj forum.dlang.org
Sometimes? In that code you write the progress variable only once, so it doesn't change after that. Nothing else can possibly happen there with or without multithreading.
Jun 27 2020