liveBook · Manning

Shared memory

As explained in Grokking Concurrency

A shared variable is a variable whose value can be accessed by multiple concurrent tasks.

Imagine a shared refrigerator: everyone in a house can put things in it or take things out. Shared memory works similarly. Concurrent tasks communicate by reading and writing common data, such as a shared variable or array. section 5.2 section 9.1

Threads in one program can share objects, and processes can use memory that appears in each process’s address space. The operating system helps allocate shared memory but does not take part in each exchange. This can make communication fast and reduce copying, especially when tasks exchange large amounts of data. section 5.2 section 5.6

Producer and consumer

A producer thread writes values into a shared array, and a consumer thread reads them. The consumer waits and retries when a value is not yet available. Both communicate through the same array rather than sending separate messages. section 5.2

# Chapter 5/shared_ipc.py
import time
from threading import Thread, current_thread

SIZE = 5
shared_memory = [-1] * SIZE                                                       #1

class Producer(Thread):
    def run(self) -> None:
        self.name = "Producer"
        global shared_memory
        for i in range(SIZE):
            print(f"{current_thread().name}: Writing {int(i)}")
            shared_memory[i - 1] = i                                              #2

class Consumer(Thread):
    def run(self) -> None:
        self.name = "Consumer"
        global shared_memory
        for i in range(SIZE):
            while True:                                                           #3
                line = shared_memory[i]                                           #3
                if line == -1:                                                    #3
                    print(f"{current_thread().name}: Data not available\n"        #3
                          f"Sleeping for 1 second before retrying")               #3
                    time.sleep(1)                                                 #3
                    continue                                                      #3
                print(f"{current_thread().name}: Read: {int(line)}")              #3
                break                                                             #3

def main() -> None:
    threads = [
        Consumer(),
        Producer(),
    ]
        
    for thread in threads:                                                        #4
        thread.start()                                                            #4
                
    for thread in threads:                                                        #5
        thread.join()                                                             #5
                
if __name__ == "__main__":
    main()

Inspect the Python threads and the shared array used for producer–consumer communication. section 5.2

Sharing also creates opportunities for conflict: two tasks may try to read or update the same data at the same time. Coordinating access with locks can protect data, but locks add overhead and can limit scalability. Message passing is an alternative that avoids sharing memory between tasks. section 5.2 section 9.10

Sharing matrices

In a concurrent matrix calculation, tasks can share matrices A and B for reading while calculating elements of C independently. This avoids giving every task its own potentially expensive copy of B. section 13.5

How Grokking Concurrency covers it

How deep it goes
Explains access, benefits and synchronization risks with a threaded implementation and a design trade-off.
Examples
Illustrates a producer and consumer sharing an array, philosophers sharing a dumpling count, and tasks sharing matrices.
What you can do after
Evaluate shared memory against copying or message passing by considering communication efficiency and synchronization costs.
How it connects
Connects shared memory to interprocess communication, message passing, locks and synchronization.

Also explained in

Compare how 5 books explain shared memory →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Concurrency. The section links point to the parts of the book it draws on.