Rust Concurrency Quiz
Master Rust's concurrency model with threads, channels, and synchronization primitives for safe parallel programming
Question 1
What is the primary concurrency model in Rust?
Question 2
What does this basic thread example do?
use std::thread;
let handle = thread::spawn(|| {
println!("Hello from thread!");
});
handle.join().unwrap();Question 3
What is the difference between thread::spawn and thread::Builder?
Question 4
What does move closure do in thread::spawn?
Question 5
What does this channel example demonstrate?
use std::sync::mpsc;
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
tx.send(42).unwrap();
});
let received = rx.recv().unwrap();Question 6
What is mpsc in std::sync::mpsc?
Question 7
What does recv() return?
Question 8
What does try_recv() do?
Question 9
What is a Mutex in Rust?
Question 10
What does this Mutex example show?
use std::sync::Mutex;
let m = Mutex::new(5);
{
let mut num = m.lock().unwrap();
*num = 6;
}
println!("m = {:?}", m);Question 11
What happens if you try to lock a Mutex from the same thread twice?
Question 12
What is RwLock?
Question 13
What does Arc stand for in concurrent contexts?
Question 14
What does this Arc<Mutex<T>> example show?
use std::sync::{Arc, Mutex};
let counter = Arc::new(Mutex::new(0));
let mut handles = vec![];
for _ in 0..10 {
let counter = Arc::clone(&counter);
let handle = thread::spawn(move || {
let mut num = counter.lock().unwrap();
*num += 1;
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}Question 15
What are the Send and Sync traits?
Question 16
Which types are not Send?
Question 17
Which types are not Sync?
Question 18
What is a data race?
Question 19
What is a race condition?
Question 20
What does thread::park() do?
Question 21
What is thread::yield_now()?
Question 22
What is a barrier?
Question 23
What does this barrier example do?
use std::sync::Barrier;
let barrier = Arc::new(Barrier::new(2));
let b1 = barrier.clone();
let handle = thread::spawn(move || {
println!("Before barrier");
b1.wait();
println!("After barrier");
});
barrier.wait();
handle.join().unwrap();Question 24
What is a Condvar (condition variable)?
Question 25
What is the difference between notify_one() and notify_all()?
Question 26
What is thread local storage?
Question 27
What does this thread_local example show?
use std::cell::RefCell;
thread_local!(static FOO: RefCell<u32> = RefCell::new(1));
FOO.with(|f| {
*f.borrow_mut() = 2;
});
let handle = thread::spawn(|| {
FOO.with(|f| {
println!("Other thread: {}", *f.borrow());
});
});
handle.join().unwrap();Question 28
What is the scoped thread API?
Question 29
What is the main advantage of message passing over shared state?
Question 30
What is the main advantage of shared state over message passing?
Question 31
What is deadlock?
Question 32
How can you prevent deadlock?
Question 33
What is lock contention?
Question 34
What is false sharing?
Question 35
What is the difference between std::thread and rayon?
Question 36
What does this rayon example do?
use rayon::prelude::*;
let sum: i32 = (0..1000).into_par_iter().map(|x| x * x).sum();Question 37
What is the key difference between concurrency and parallelism?
Question 38
What is the thread pool pattern?
Question 39
What is cooperative vs preemptive scheduling?
Question 40
In a web server handling multiple concurrent requests, each needing to update shared user session data and log to a shared file, which concurrency approach would you choose and why?
