std.parallelism Question
dsimcha
dsimcha at yahoo.com
Tue Apr 19 09:43:55 PDT 2011
== Quote from Jonathan Crapuchettes (jcrapuchettes at gmail.com)'s article
> I've been looking at your std.parallelism module and am really interested in it,
> but there is one problem that I have run into that I can't seem to find a
> solution. If I have code that requires a database connection in each thread, I
> don't really want to be creating and then deleting a lot (e.g. 500,000) of
> connections while using taskPool.parallel. Normally I would create a set of
> objects that extend Thread and create a single connection in each. Can you offer
> a suggestion how I might handle this situation?
These kinds of situations are **exactly** what worker-local storage is for. Roughly:
// new MysqlConnection() is lazy, you get one per worker thread.
auto connections = taskPool.workerLocalStorage(new MysqlConnection());
foreach(element; taskPool.parallel(array)) {
// Get the worker-local instance of connection.
auto connection = connections.get;
// Do stuff.
}
// Now that we're done with the parallel part of the algorithm,
// we can access all the connections to free them.
foreach(c; connections.toRange) {
c.free();
}
More information about the Digitalmars-d
mailing list