I am currently implementing an application that uses multithreading but has requirements for overall memory consumption. I would like the main thread to do I / O, and several workers to do the calculations.
Currently, I have several data structures in the stack of wizards that workers access. I use OpenMP to distribute work. Since the master / worker pattern does not work with OpenMP, I would like to use pthreads for multithreading.
I know that each thread supports a local stack, but what exactly will happen to the stack when a thread is created?
Are the repositories that are on the master stack available to workers, or should I move them to a heap? I would also like to avoid data duplication, but I don't know if the new threads will create a local copy of the master stack.
Edit: found the answer myself ...
After reading the details of the clone () system call used by pthreads, I realized that all threads share the full virtual memory. This means that although threads use their own stack, the memory areas that are used for each stack are still split.
I wrote code to test this behavior:
#include <stdio.h> #include <pthread.h> void* increment(void* value) { int* val = (int*) value; for(int i = 0; i < 100; i++) { ++(*val); } return 0; } int main(int argc, char** argv) { int stackvar = 0; pthread_t thread1, thread2; int iret1, iret2; iret1 = pthread_create( &thread1, NULL, increment, (void*) &stackvar ); iret2 = pthread_create( &thread2, NULL, increment, (void*) &stackvar ); pthread_join( thread1, NULL ); pthread_join( thread2, NULL ); printf("%i\n", stackvar); return 0; }
As "200" output, threads successfully process the stack of their parent thread.
I think most ressources on the Internet do not express this fact correctly. Threads do share the stack in the sense of shared memory, but the stack pointer of each thread is private. For each thread, a portion of the shared memory is assigned as a local stack.
It also means that it doesn't matter if the parent stream has large data structures on the stack, since the memory is never duplicated for streaming.