Search code examples
c++exceptionmemoryparallel-processingtbb

Safe Memory allocation during a c++ parallel algorithms invocation (with Intel TBB)?


I would like to understand whether a memory allocation inside a function which a thread executes is safe for the c++ parallel algorithms. Consider the following situation:

std::for_each(std::execution::par, first, last, func),

with func being the function object. During a call to func (CPU and GPU) memory is allocated. When too many threads execute and allocate memory, the system would run out of memory and throw an exception. So far, this has not happened. I wonder if I was merely lucky or whether the execution policy considers such memory constraints or catches memory-related exceptions? The parallel algorithm is implemented through Intel TBB.


Solution

  • You can use tasks instead of threads as Intel oneTBB works on tasks instead of low-level threads that is, it creates tasks instead of thread and it will map these tasks onto hardware at runtime. The Task scheduler takes care of mapping the tasks onto threads. You should explicitly free the memory space when we use for_each in the source code as suggested by Jerome Richard.