Home > Net >  How does the stack work in multithreaded programs using Pthread?
How does the stack work in multithreaded programs using Pthread?

Time:09-17

I'm having a simple question, I believe, as far as I know, a multithreaded program, they share the process's memory space between all threads, that includes, stack, global memory area, file descriptors, etc., I I want to know why in the first example, there is a consistency problem, since theoretically all threads share the stack, in the second example, the race problem occurs.

#include <stdio.h>
#include <pthread.h>

void *thr(void *arg)
{
   for(int i = 0; i < 50; i  )
       printf("Thread = %zu Value= %d\n", pthread_self(), i);
   return NULL;
}

int main(void)
{
   pthread_t threads[2];
   for(int i = 0; i < 2; i  )
     pthread_create(&threads[i], NULL, thr, NULL);
   for(int i = 0; i < 2; i  )
     pthread_join(threads[i], NULL);
   return 0;
}

Second program with running problem

#include <stdio.h>
#include <pthread.h>

int i = 0;

void *thr(void *arg)
{
   for(; i < 50; i  )
     printf("Thread = %zu Value= %d\n", pthread_self(), i);
   return NULL;
}

int main(void)
{
   pthread_t threads[2];
   for(int i = 0; i < 2; i  )
     pthread_create(&threads[i], NULL, thr, NULL);
   for(int i = 0; i < 2; i  )
     pthread_join(threads[i], NULL);
   return 0;
}

3 example with race problem, in this case the variable is created in the main thread and is passed as argument to the function, that is, the only shared stack is from the main thread ?

#include <stdio.h>
#include <pthread.h>

void *thr(void *arg)
{
   int *ptr = (int *)arg;
   for(; *ptr < 50; (*ptr)  )
     printf("Thread = %zu Value= %d\n", pthread_self(), *ptr);
   return NULL;
}

int main(void)
{
   int i = 0;
   pthread_t threads[2];
   for(int i = 0; i < 2; i  )
     pthread_create(&threads[i], NULL, thr, &i);
   for(int i = 0; i < 2; i  )
     pthread_join(threads[i], NULL);
   return 0;
}

CodePudding user response:

share the process's memory space between all threads, that includes, stack

Well, yes and no.

There's a difference between sharing a memory address space and a specific region of memory.

While it is true that all threads share an address space, each thread has its own stack (allocation of memory).

A shared address space means that a given virtual address (pointer value) refers to the same physical memory in all threads.

But a dedicated stack means that each thread's stack pointer started off at a different place in that address space, so as to not conflict which each other.

CodePudding user response:

Every thread in a process has normally its own stack pointer (meaning it has its own stack).

This means that while they share the virtual address space, giving them access to other thread's variables or even the stack, they normally have their own stacks in different, non overlapping places of this shared virtual memory space.

When the kernel schedules a thread, it installs the thread's registers in the core or cpu it is going to use (and this includes the stack pointer with a value of stack for each thread) Indeed, the kernel maintains a user mode stack for each thread and also a kernel stack (this last to allow two different threads of the same process to be executing a system call at the same time --the same or different--, which would not be possible if they had to share a single stack)

  • Related