OS Threads and Locks
Rust's type system makes data races a compile error through Send and Sync. Interviewers test spawning, Arc with Mutex, channels, atomics and data parallelism.
on this pageshowhide
explore
- Spawning and Scopes5 questions
- Send and Sync5 questions
- Mutex, RwLock and Arc6 questions
- Message-Passing Channels5 questions
- Atomics and Memory Ordering6 questions
- Rayon Parallel Iterators5 questions
questions
page 2 of 2As a Rust lead, would you keep one `Mutex<HashMap>` for a hot rate limiter, shard it, or standardise on `parking_lot::Mutex`, and how would you decide?
level: principalimportance: should knowfreq 30%
basics
~20 sKeep one Mutex<HashMap> until profiles show lock contention, shrink the critical section first, then shard by client hash with one lock per request. Default to std's lock and admit parking_lot only for a measured need or a feature std lacks.
In stable Rust, how do you stop a struct holding only a `u32` index into a per-thread registry from being `Send` and `Sync`?
level: middleimportance: nice to knowfreq 25%
basics
~20 sAdd a zero-sized PhantomData<*const ()> field. PhantomData<T> carries T's auto traits, and raw pointers are neither Send nor Sync, so the struct loses both; impl !Send is a negative impl, which stable rustc rejects with E0658.
showing 31–32 of 32