skip to content

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 pageshow

explore

questions

page 2 of 2

As 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 s

Keep 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.

open as a page

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 s

Add 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.

open as a page

showing 31–32 of 32