How do I limit the number of active threads (queuing spawn calls)
Brad Roberts
braddr at puremagic.com
Sat Mar 26 19:12:51 PDT 2011
On 3/26/2011 7:00 PM, Andrej Mitrovic wrote:
> Edit: It looks like I did almost the same as Jonathan advised.
>
> I'm looking forward to std.parallelism though. I'm thinking I'd
> probably use some kind of parallel foreach loop that iterates over 4
> files at once, and letting it do its work by spawning 4 threads. Or
> something like that. We'll see.
The way I've typically done this sort of pattern is with a thread pool that gets its work from a queue. The main thread
shoves work into the queue and then calls a .join or .waitForEmpty sort of api on the pool. So it'd look something like:
void workerFunc(string str) { ... }
auto tp = new ThreadPool(getNumCpus(), &workerFunc);
foreach(...)
tp.push(str);
tp.join();
This can suffer from queue size problems if the amount of work is awful, but that's not a problem for the vast majority
of the cases I've had, so never worried about having the push capable of blocking or otherwise throttling the producer side.
More information about the Digitalmars-d-learn
mailing list