Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do two or more threads share memory on the heap that they have allocated?

As the title says, how do two or more threads share memory on the heap that they have allocated? I've been thinking about it and I can't figure out how they can do it. Here is my understanding of the process, presumably I am wrong somewhere.

Any thread can add or remove a given number of bytes on the heap by making a system call which returns a pointer to this data, presumably by writing to a register which the thread can then copy to the stack. So two threads A and B can allocate as much memory as they want. But I don't see how thread A could know where the memory that thread B has allocated is located. Nor do I know how either thread could know where the other thread's stack is located. Multi-threaded programs share the heap and, I believe, can access one another's stack but I can't figure out how.

I tried searching for this question but only found language specific versions that abstract away the details.

Edit: I am trying not to be language or OS specific but I am using Linux and am looking at it from a low level perspective, assembly I guess.

like image 391
Lemma Prism Avatar asked Aug 10 '12 21:08

Lemma Prism


People also ask

Do threads share heap memory?

It is important to distinguish between these two types of process memory because each thread will have its own stack, but all the threads in a process will share the heap.

Can multiple threads share the same heap?

No. There is a single heap that is shared by all threads in the Java process.

Can two threads share the same memory?

In a multi-threaded process, all of the process' threads share the same memory and open files. Within the shared memory, each thread gets its own stack. Each thread has its own instruction pointer and registers.


2 Answers

My interpretation of your question: How can thread A get to know a pointer to the memory B is using? How can they exchange data?

Answer: They usually start with a common pointer to a common memory area. That allows them to exchange other data including pointers to other data with each other.

Example:

  1. Main thread allocates some shared memory and stores its location in p
  2. Main thread starts two worker threads, passing the pointer p to them
  3. The workers can now use p and work on the data pointed to by p

And in a real language (C#) it looks like this:

//start function ThreadProc and pass someData to it
new Thread(ThreadProc).Start(someData)

Threads usually do not access each others stack. Everything starts from one pointer passed to the thread procedure.


Creating a thread is an OS function. It works like this:

  1. The application calls the OS using the standard ABI/API
  2. The OS allocates stack memory and internal data structures
  3. The OS "forges" the first stack frame: It sets the instruction pointer to ThreadProc and "pushes" someData onto the stack. I say "forge" because this first stack frame does not arise naturally but is created by the OS artificially.
  4. The OS schedules the thread. ThreadProc does not know it has been setup on a fresh stack. All it knows is that someData is at the usual stack position where it would expect it.

And that is how someData arrives in ThreadProc. This is the way the first, initial data item is shared. Steps 1-3 are executed synchronously by the parent thread. 4 happens on the child thread.

like image 85
usr Avatar answered Dec 01 '22 17:12

usr


A really short answer from a bird's view (1000 miles above):
Threads are execution paths of the same process, and the heap actually belongs to the process (and as a result shared by the threads). Each threads just needs its own stack to function as a separate unit of work.

like image 27
Cratylus Avatar answered Dec 01 '22 16:12

Cratylus