On Thursday, 19 May 2016 at 10:58:42 UTC, Rene Zwanenburg wrote:
Calling task() only creates a Task, you also have to start it
somehow. The documentation contains an example:
https://dlang.org/phobos/std_parallelism.html#.task
I should add that a single shared array will cause contention if
the number of calls to addLine is large compared to the amount of
work done.
If performance is a problem, another way is to use a thread local
array and merge the arrays to a single global one on thread
termination. Something like this:
// thread local. Can be appended to without locking
string[] lines;
__gshared Mutex mutex;
shared static this()
{
mutex = new Mutex();
}
__gshared string allLines;
static ~this()
{
synchronized(mutex)
{
allLines ~= lines;
}
}
This will not preserve order of course, and you'll have to make
sure all your worker threads are terminated before using
allLines. Both can be fixed if required but will make the code
more complicated.
The upside is that this will avoid contention on the mutex.
You'll still have to be careful with the GC though, just about
every GC operation takes a global lock so it doesn't play nice
with high perf multi-threaded code.