Overview

While often used interchangeably, concurrency and parallelism are different. Concurrency is about dealing with many things at once (structure), while Parallelism is about doing many things at once (execution).

Core Concepts

  • Process vs. Thread:
    • Process: An independent program with its own memory space.
    • Thread: A “lightweight” unit of execution within a process; threads share the same memory.
  • Async/Await: A pattern for handling non-blocking operations (I/O, API calls) without freezing the main execution thread.
  • Race Condition: Occurs when two or more threads access shared data and try to change it at the same time, leading to unpredictable results.
  • Deadlock: A situation where two threads are waiting for each other to release a resource, causing both to hang forever.
  • Synchronization Primitives:
    • Mutex (Mutual Exclusion): Ensures only one thread can access a resource at a time.
    • Semaphore: Allows a limited number of threads to access a resource.

Code Examples

// Async/Await (Concurrency in JS/TS)
async function fetchData() {
    try {
        const response = await fetch("https://api.example.com/data");
        const data = await response.json();
        console.log(data);
    } catch (e) {
        console.error("Fetch failed");
    }
}

fetchData(); 
console.log("This runs while data is fetching!"); // Non-blocking

Use Cases

  • Web Servers: Handling thousands of simultaneous user requests.
  • Video Rendering: Splitting a frame into chunks and rendering them in parallel across multiple CPU cores.
  • Real-time Systems: Processing sensor data while simultaneously updating a UI.

Gotchas

  • The Event Loop: In single-threaded environments like Node.js, a heavy CPU-bound loop will block all other requests, effectively killing the server.
  • Shared State: The most dangerous part of parallelism. Always prefer “immutability” or “message passing” over shared mutable state.

Related Notes