Creating a thread pool in a function call and using from another function call

advertisements

I have a Fortran program that calls a C++ dll to do some mathematical operations on 10000 sets of data. The data sets are totally independent from each other. I was planning to create a thread pool and then send tasks to it. However, the call to the dll will be made more than 1000 times (each call the 10000 sets of data are being processed).

My question is: when I create the thread pool during the first call to the dll, what happens to this thread pool after the function in the dll returns ? Can the second call (and the remaining 998 calls) access the pool that was created during the first call.


You can indeed use the same thread pool, if you set things up right.

Objects created on the stack of the FORTRAN->C++ calling thread will be destroyed as that stack unwinds and control returns to FORTRAN, so it's not a good idea to have the thread pool management data on that stack. You can, however:

  • launch another thread that creates the thread pool management data/object, or
  • allocate on the heap (using new) to decouple lifetime from the FORTRAN->C++ calls.

The latter is probably easier and cleaner... a pointer to the heap object/data managing the thread pool can be returned to FORTRAN and used as a "handle" for future calls, indicating the same thread pool should be used.