Overview
4 min read
This section descends from high-level application code to the systems layer, where Rust replaces C and C++ for a TypeScript/JavaScript developer who has only ever had a single-threaded event loop. You will spawn real OS threads, fan work across every CPU core with Rayon, pass messages over channels, reach for lock-free atomics with explicit memory ordering, and work directly with the file system, raw sockets, Unix signals, and child processes. Throughout, the recurring theme is fearless concurrency: the same ownership rules that protect a single thread also turn data races into compile errors, so code that would segfault in C++ or silently corrupt a SharedArrayBuffer in JavaScript simply does not build.
What You’ll Learn
Section titled “What You’ll Learn”- Spawning, joining, and scoping native OS threads with
std::thread, and how they differ from JavaScript Web Workers. - Reusing a fixed pool of worker threads with Rayon, including the global pool,
rayon::join, and custom pools. - Turning sequential iterators into parallel ones with
par_iter()/par_bridge()— and judging when parallelism actually pays off. - Passing values between threads safely over
std::sync::mpscandcrossbeam-channel, with backpressure andselect!. - Sharing counters and flags without a lock using atomic types (
AtomicUsize,AtomicBool) and compare-and-swap. - Choosing the right memory ordering (
Relaxed/Acquire/Release/AcqRel/SeqCst) and understanding what each one guarantees. - Going beyond
read_to_string: metadata, Unix permission bits, symlinks, directory walking, and memory-mapping. - Building blocking TCP and UDP servers and clients with
std::net. - Catching
SIGINT/SIGTERMfor clean shutdown withctrlcandsignal-hook. - Spawning and managing subprocesses with
std::process::Command— arguments, environment, pipes, and exit status.
Topics
Section titled “Topics”| Topic | Description |
|---|---|
| Native Threads | std::thread: spawn/join, move closures, and scoped threads (std::thread::scope), versus JavaScript Web Workers. |
| Thread Pools with Rayon | Reusing worker threads with Rayon: the global pool, rayon::join, and custom ThreadPoolBuilder pools. |
| Parallel Iterators | Data parallelism with par_iter()/par_bridge(); when it helps and when it does not. |
| Channels | std::sync::mpsc (and crossbeam-channel): producer/consumer message passing across threads. |
| Atomic Operations | Atomic types (AtomicUsize/AtomicBool): load/store, fetch_add, and compare_exchange. |
| Memory Ordering | Relaxed/Acquire/Release/AcqRel/SeqCst — what each guarantees, with runnable examples. |
| Advanced File-System Operations | Metadata, permissions, symlinks, directory walking with walkdir, and memory-mapping with memmap2. |
| Low-Level Networking | std::net: TcpListener/TcpStream/UdpSocket and a tiny echo server. |
| Signal Handling | Clean shutdown on SIGINT/SIGTERM with ctrlc and signal-hook. |
| Process Management | Spawning and managing subprocesses with std::process::Command: args, env, pipes, and status. |
Learning Objectives
Section titled “Learning Objectives”By the end of this section, you will be able to:
- Decide when to use raw threads, a Rayon pool, channels, or atomics for a given concurrency problem.
- Spawn threads that either own (
move) or borrow (thread::scope) their data, and join them to collect results or detect panics. - Parallelize a CPU-bound pipeline by changing
iter()topar_iter(), and benchmark it honestly in--release. - Build a producer/consumer pipeline with bounded channels and reason about who holds the last sender.
- Implement a lock-free counter, flag, or CAS loop and pick the weakest correct memory ordering.
- Inspect file metadata and permissions, follow (or refuse to follow) symlinks, walk a tree while pruning subdirectories, and memory-map a file safely.
- Write a blocking TCP/UDP server that frames messages correctly and handles errors by
ErrorKind. - Turn
SIGINT/SIGTERMinto an orderly shutdown by flipping anAtomicBooland polling it. - Launch external programs, capture or stream their output, and distinguish “could not start” from “ran but failed.”
Prerequisites
Section titled “Prerequisites”This is an advanced section. Before starting, you should be comfortable with:
- Section 05: Ownership — moves, borrows, and lifetimes are the foundation of
Send/Syncand scoped threads. - Section 10: Smart Pointers —
Arc,Mutex, and interior mutability underpin shared state across threads. - Section 11: Async Programming — async tasks are not threads; knowing the difference clarifies when to use each.
Estimated Time
Section titled “Estimated Time”14 hours — roughly 8 hours of reading and worked examples plus 6 hours on the exercises. Threads, channels, and atomics reward hands-on experimentation, so budget time to run the code and watch the non-deterministic interleavings yourself.
Next: Section 27: Security →