On 9/12/2011 3:32 PM, Jerry Quinn wrote:

The other thing I think I have to do is sequence the initializations.  
Otherwise the disk/network will thrash trying to load N copies simultaneously.  
I imagine this can be done with a shared variable inside the engine.


This can easily be done by creating null engines in the WorkerLocalStorage and then initializing them inside a synchronized block:

auto wlEngines = taskPool.workerLocalStorage(Engine.init);
auto wlIsInitialized = taskPool.workerLocalStorage(false);

// This gets executed on the pool in a Task:
void taskFunction() {
    if(!wlIsInitialized.get) {
        // Do initialization of thread-local engine in worker thread.
        synchronized wlEngines.get = new Engine(ctorArgs);
        wlIsInitialized.get = true;
    }

    // Do main processing.
}

I prefer to do all the initializations before work starts so that timing can be 
more accurate.  These things take a lot to get fully loaded and ready to run.

Unfortunately I don't have a good answer for this constraint. If you can offer a concrete enhancement request for std.parallelism, I'll certainly consider it, but I can't think of a good design for this kind of per-thread initialization routine off the top of my head, either in terms of API or implementation.

Reply via email to