Accessing the local linux thread stack (pthreads)

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.

+4
source share
1 answer

Yes, you can share your stacks, as you explain. In general, however, you should not, except for a few special circumstances. Your sample program has data races, and so the output will be only 200 - due to pure luck (perhaps due to the fact that your operating system does not actually plan to launch children at the same time.)

One of the special circumstances when it makes sense to split the stack is that the parent creates some interesting data structure and then passes it to its children, which access only the data structure.

Aside from the data race, there is one more thing here that might go different than the stack instead of the heap. The parent can return from the procedure in which he created the child threads, so the stack data may not remain valid for the lifetime of the child thread. Thus, you should only share stack data if the pthread_create() call and the corresponding pthread_join() call are in the same area (as in your example.)

+4
source

Source: https://habr.com/ru/post/1502888/


All Articles