I am needing some per thread scratchmemory where I can store some temporary values. This scratchmemory needs to be fairly large (several MB or so), so I dont want to allocate that on the stack.
I guess this is what thread local storage is for but I have some trouble understanding how I can use the TBB combinable classes for it and which exactly I would need.
Are there any examples on how to use it? Assuming I have a main thread that executes several tasks, how and when do I allocate the memory? How do I access the memory? Do I need memory per Task-Type or can I have something like a global scratchmemory my different tasks can access to store temporary values?
Creates a thread private pointer named pYourStruct and initialized to NULL. Then whenever your code dereferences the pYourStruct, it generates some additional code to obtain it from a thread private area. You can see this code in a dissassembly window.
Each thread will have a private copy of the pointer (and everything else you decorate with __declspec( thread )). Each thread that uses this poiner must initialize the pointer and any corrisponding allocations within the struct.
You can use this technique with OpenMP theads, PThreads, Cilk++ threads, etc...
*** g++ uses a different syntax to mark threads private