Video summary
Lecture 2: RPC and Threads
Main summary
Key takeaways
Why the Course Uses Go
The instructor says Go is useful for distributed-systems labs because it offers:
- Built-in support for threads, called goroutines in Go, and synchronization.
- A convenient remote procedure call (RPC) package, useful when programs on different machines need to communicate.
- Type and memory safety, which help prevent bugs involving accidental memory corruption.
- Garbage collection, which avoids manual memory management. This is especially helpful with threads, where it can otherwise be difficult to know when shared memory is safe to free.
- A relatively simple language and clearer compiler errors than C++.
Students are directed to Effective Go as a resource for learning the language.
Threads and Concurrency
A program runs in an address space. A single-threaded program has one execution path; a threaded program can have multiple paths, each with its own program counter, registers, and stack. The threads share the program’s address space and can access common data. Go’s main function also runs as a goroutine.
The lecture describes three reasons to use threads:
- I/O concurrency: A thread can wait for a disk operation, network reply, or RPC while other threads continue working. For example, a client can issue requests to several servers and wait for their replies concurrently.
- CPU parallelism: On a multi-core machine, multiple threads doing compute-heavy work can run at the same time on different cores.
- Background or periodic work: A goroutine can sleep and periodically perform a task, such as checking whether workers are still alive.
Threads have some overhead, especially when there are very large numbers of them, but a modest number is usually inexpensive. Programs can still create too many threads or fail to stop them, so their number and lifetimes should be managed.
Event-Driven Programs and Processes
An alternative to threads is event-driven programming: a single thread waits in a loop for events, then uses stored state to handle each one. This can be more memory-efficient than creating a thread for every client or activity, but it is harder to program and does not, by itself, make use of multiple CPU cores.
A Unix process is a program with its own address space. Multiple threads within a process share memory; separate processes generally do not. The operating system schedules threads, while Go may also schedule multiple goroutines over a smaller number of operating-system threads.
Shared Memory, Races, and Locks
Shared memory is convenient, but concurrent access can cause race conditions. A simple example is two threads incrementing the same variable. An increment consists of multiple steps—read, add, write—so both threads could read the same old value and overwrite one another’s updates.
The usual solution is a mutex:
- Acquire the lock before accessing or updating shared data.
- Release it afterward.
- Ensure every thread that accesses the protected data follows the same locking plan.
Go does not automatically associate a mutex with particular variables or data structures. The programmer must decide what the lock protects. Hiding a lock inside a data structure can be convenient, but interactions between separately locked structures can create deadlocks.
The instructor warns that race-prone code may appear to work in simple tests. Go’s race detector, enabled with the -race flag, can report conflicting reads and writes observed during a run. It is useful but does not inspect code that the run never executes, so testing needs to exercise relevant paths.
Coordination and Deadlocks
Locks protect shared data; coordination is when threads deliberately wait for or notify one another. Go provides several mechanisms:
- Channels for sending information between goroutines.
- Condition variables for notifying a thread that may be waiting.
- Wait groups for waiting until a known set of goroutines finishes.
A deadlock occurs when threads wait indefinitely for one another. A common example is two threads each holding one lock and waiting for the other lock. A program that stops making progress without crashing may be deadlocked.
Web Crawler Examples
The crawler illustrates different approaches to concurrency. It must avoid fetching the same URL more than once, explore links without looping forever, fetch pages concurrently for better throughput, and detect when the crawl is finished.
1. Serial Crawler
- Performs a depth-first traversal of the pages.
- Keeps a
fetchedmap as a set of URLs already visited. - Returns immediately for a URL already in the map; otherwise, it records and fetches the URL, then recursively visits the links.
- Works as a basic traversal but fetches pages one at a time.
2. Naive Goroutine Version
- Simply starts each child crawl as a goroutine.
- Does not wait for those goroutines to finish.
- The parent can return—and the program can exit—before they do useful work, so merely adding
godoes not produce a correct concurrent crawler.
3. Concurrent Crawler with Shared State
- Starts a goroutine for each crawl while sharing a URL table among them.
- Uses a mutex to make checking and marking a URL atomic; without it, two goroutines could both decide the same URL has not been fetched and fetch it twice.
- Uses a wait group to count outstanding work: add when starting a goroutine, call
Donewhen it finishes, and wait for the count to reach zero. - The lecture notes that
deferis a safer way to ensureDoneruns when a goroutine exits. - Passes each URL as an argument to the goroutine so it gets its own copy. Otherwise, a goroutine might observe the loop variable after it has changed.
- Demonstrates shared-state synchronization, but has no clear upper bound on the number of goroutines. A worker pool can limit concurrency.
4. Concurrent Crawler Using Channels
- Keeps the fetched-URL map private to a master goroutine rather than sharing it among workers.
- The master starts workers to fetch pages; each worker sends the page’s links back over a channel.
- The master receives results, records newly found URLs, and starts more workers as needed.
- Since workers communicate results instead of accessing shared crawl state, this design avoids a shared map protected by a mutex.
- The master tracks outstanding workers and finishes when the count reaches zero. The example seeds the channel with the initial URL so the master does not wait forever before any work begins.
- Channels support concurrent sending and receiving; Go’s channel operations handle their internal synchronization.
Overall, the examples contrast two common approaches: protect shared data with locks, or keep data private and coordinate by passing messages.
Speakers and Sources Featured
- Instructor: The MIT 6.824 lecturer, whose name is not stated in the subtitles.
- Students: Unidentified students ask questions during the lecture.
- Sources and materials referenced: Go documentation; Effective Go; the course tutorial; the
crawler.goexample on the course website; Go’s built-in race detector; and, as a comparison, POSIX threads (“pthreads”) for C.
Rate this summary
Your feedback will help improve summaries.
Improve this summary
Reprocess with a stronger model when the summary feels incomplete or inaccurate.
Translate summary in another language
Ask questions to this video
Chat for follow-up questions, clarifications, and source-backed answers.