I'm currently implementing an application that makes use of multithreading but has requirements on the total memory consumption. I'd like to have a master thread doing I/O and several workers doing computations.
Currently I'm having several datastructures on the masters stack that are accessed by the workers. I use OpenMP for work distribution. As the master/worker pattern doesn't work well with OpenMP I'd like to use pthreads for multithreading.
I know that each thread maintains a local stack, but what exactly will happening to the stack on thread creation?
Are datastructures that lie on the master's stack accessible by the workers or should I move them to the heap? I'd also like to avoid duplication of data but don't know if new threads will create a local copy of the masters stack.
Edit: found the answer myself ...
After reading into the details of the clone() system call that is used by pthreads I realized that all threads share the complete virtual memory. This means that although threads use their own stack, the memory regions that are used for each stack are still shared.
I wrote some code to verify that behaviour:
#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 output is "200" the threads manipulated the stack of their parent thread successfully.
I think most ressources on the internet dont't express this fact correctly. Threads do share the stack in the sense of shared memory, but the stackpointer of each thread is private. For each thread a part of the shared memory is assigned as local stack.
This also means that it doesn't matter if the parent thread has large data structures on the stack, as memory is never duplicated for threading.