multithreading
memory allocation
heap memory
thread synchronization
concurrent programming

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

Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms

Introduction

In concurrent programming, the ability for multiple threads to share memory is both a powerful feature and a source of many potential issues, such as race conditions and deadlocks. Understanding how threads share memory on the heap is critical for building efficient and correct multi-threaded applications. This article dives into the mechanisms by which threads share heap memory, explores common patterns, and provides insights into synchronization techniques.

Threads and the Heap

By definition, the heap is a region of memory dynamically allocated to processes during runtime. Unlike the stack, which is specific to a single thread, the heap is shared across all threads within the same process. This shared nature allows threads to see and modify the same variables and objects in memory.

How Threads Share Memory

  1. Pointer Sharing:
    • Threads share memory by having access to the same pointer to a memory location on the heap. When, for example, an object is allocated on the heap, multiple threads can be provided with the address of that object. This allows them to read and write to that memory location concurrently.
    • Languages like Java, C++, and Python provide container classes (like lists, arrays, maps) that can be shared among threads. These containers can be stored on the heap, and threads can access them concurrently.
    • Objects or data structures created in a shared library that multiple threads access can also be on the heap. These require careful synchronization to prevent race conditions.
    • A mutex is used to enforce exclusive access to shared resources. Only one thread can hold the mutex at a time to access the shared memory.
    • Atomics provide a way to perform operations in a way that appears to be instantaneous from an external observer. They do not require locks.
    • When read operations are more frequent than writes, read-write locks allow multiple threads to read simultaneously while still synchronizing write access.
    • Used to synchronize the progress of multiple threads, ensuring that certain points in execution are reached before proceeding.
  • Deadlocks: When each thread waits for a resource held by another, resulting in a situation where no progress is possible.
  • Priority Inversion: Occurs when lower-priority threads hold a lock required by higher-priority threads.
  • Cache Coherency: In multi-core systems, caches can become inconsistent, requiring proper memory barriers or hardware solutions.

Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms