liveBook · Manning

Thread

As explained in Latency

A thread is a schedulable sequence of instructions that represents a unit of execution within a process.

A thread is an independent stream of instructions executed within a process. Each thread has its own stack, while threads in the same process share memory and other resources. Shared-resource access must be synchronized to avoid races and data corruption. section 7.5.1 section 9.2.1

The operating system schedules threads onto CPUs. On one core, it can switch between threads so each makes progress without running simultaneously; on multiple cores, threads can run in parallel. Real schedulers use more complex rules than fixed time slices, and a thread waiting for I/O may stop running sooner. section 7.5.1 section 9.2.1

Figure 9.2 The OS runs multiple threads at the same time (but not simultaneously) on a single CPU core by context-switching between them. In this example, every thread is run for exactly 4 milliseconds, and then the OS switches to another thread. In real-world systems, a thread might block to wait for I/O, for example, causing the OS to switch to another thread more quickly.
Figure 9.2: The OS runs multiple threads at the same time (but not simultaneously) on a single CPU core by context switching between then.

Follow three threads as they take turns on one core in a simplified four-millisecond scheduling example. section 9.2.1

Scheduling matters for latency: even a ready thread runs only when the OS schedules it. A network request, for example, waits until its processing thread runs. Avoiding too many threads can help; in some cases, CPU affinity can restrict application threads to specified CPUs using pthread_setaffinity_np(). section 7.5.1

Thread APIs provide a practical way to start concurrent work. Rust’s std::thread::spawn() accepts a closure and returns a handle; the example creates ten threads and joins their handles. On Linux, thread management typically relies on POSIX thread primitives such as pthread_create(). section 9.2.1

fn main() {
    let mut threads = vec![];
    for i in 0..10 {
        let t = std::thread::spawn(move || {
            println!("Hello from thread {}", i);
        });
        threads.push(t);
    }
    for t in threads {
        t.join().unwrap();
    }
}

Inspect the Rust example that spawns ten threads and joins each returned handle. section 9.2.1

How Latency covers it

How deep it goes
Explains thread resources, scheduling and creation APIs, without detailing real scheduler algorithms.
Examples
Illustrates three threads sharing one core and a Rust program spawning and joining ten threads.
What you can do after
Apply thread creation and joining with Rust’s standard library, and configure CPU affinity through pthread_setaffinity_np().
How it connects
Connects threads to processes, concurrency, parallelism, context switching, mutual exclusion and CPU affinity.

Also explained in

Compare how 12 books explain thread →

Related concepts

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