How to specify number of worker threads for Taskpool

Ali Çehreli acehreli at yahoo.com
Sun Feb 17 09:34:55 PST 2013


On 02/17/2013 08:23 AM, Sparsh Mittal wrote:
 > I am seeing
 >
 > http://dlang.org/phobos/std_parallelism.html#.TaskPool

Please also consider:

   http://ddili.org/ders/d.en/parallelism.html

 > I am running the example of parallel foreach given in that section.
 >
 > Sorry if it is too obvious, but my question is when using
 >
 >
 > foreach(i, ref elem; taskPool.parallel(logs)) { elem = log(i + 1.0); }
 >
 > where taskPool is not explicitly defined,

That is a pre-defined object of the std.parallelism module.

 > how do I specify number o
 > worker threads?

You cannot change taskPool. It is started with one less than the total 
cores of the system.

 > I don't know when is constructor of TaskPool called and
 > hence when
 >
 > /////// @trusted this(size_t nWorkers);
 > /////// Allows for custom number of worker threads.
 >
 > function which allows for setting number of threads, is called.
 >
 > I will be thankful for your help.

You must construct a separate TaskPool object and use that one for your 
special needs. The following example from the link above uses just 2 cores:

import std.stdio;
import std.parallelism;

void main()
{
     auto workers = new TaskPool(2);

     foreach (i; workers.parallel([1, 2, 3, 4])) {
         writefln("Working on %s", i);
     }

     workers.finish();
}

Ali



More information about the Digitalmars-d-learn mailing list