Concurrency Quiz - Languages Rust
Free practice quiz: Concurrency (Languages Rust). 55 multiple-choice questions with answers and explanations. Play online, no sign-up needed.
Play all 55 questions freeSample questions
What fundamental primitive does Rust use to manage shared mutable state across threads without data races?
- Mutex
- RwLock
- RefCell
- Atomic types
Show answer
Answer: A. Mutex
Rust's `std::sync::Mutex` is the primary primitive for protecting shared mutable data. It ensures that only one thread can access the data at a time, preventing data races. `RwLock` allows multiple readers or one writer. `RefCell` is for interior mutability but is not thread-safe. Atomic types are for simple, lock-free operations on primitive types.
What is the primary mechanism Rust's `Mutex` uses to ensure mutual exclusion?
- Operating system primitives (e.g., futex on Linux)
- Busy-waiting loops
- Spinlocks
- Signal handling
Show answer
Answer: A. Operating system primitives (e.g., futex on Linux)
Rust's standard library `Mutex` typically relies on underlying operating system primitives (like `futex` on Linux, `Windows Mutex` on Windows) to efficiently manage thread blocking and waking. This is more efficient than pure busy-waiting or spinlocks, which consume CPU cycles unnecessarily.
Which method is used to create a channel that supports multiple producers and multiple consumers in Rust?
- `crossbeam_channel::unbounded()`
- `std::sync::mpsc::channel()`
- `std::sync::mpsc::sync_channel()`
- `std::thread::spawn()`
Show answer
Answer: A. `crossbeam_channel::unbounded()`
While `std::sync::mpsc` is Multiple Producer Single Consumer, the `crossbeam-channel` crate provides more flexible channel types. `crossbeam_channel::unbounded()` can create channels that support multiple producers and multiple consumers. `std::sync::mpsc::channel()` is MPSC, and `sync_channel` has a bound on the number of messages.
What does the `TryLockError` indicate when returned by a `try_lock()` method on a `Mutex` or `RwLock`?
- The lock could not be acquired immediately because another thread holds it.
- The lock was successfully acquired.
- The `Mutex` or `RwLock` is poisoned.
- The thread panicked while holding the lock.
Show answer
Answer: A. The lock could not be acquired immediately because another thread holds it.
`try_lock()` attempts to acquire a lock without blocking. If the lock is already held by another thread, it returns a `TryLockError` (or similar) indicating that the lock is currently unavailable. It does not imply poisoning or successful acquisition.
What is the purpose of the `SendSync` type alias in the `atomix` crate (or similar concepts)?
- To represent types that are both `Send` and `Sync`.
- To represent types that are only `Send`.
- To represent types that are only `Sync`.
- To represent types that are neither `Send` nor `Sync`.
Show answer
Answer: A. To represent types that are both `Send` and `Sync`.
A `SendSync` type alias or trait bound is a common convention in Rust concurrency to explicitly state that a type must be safe to send to another thread (`Send`) and safe to share via immutable references across threads (`Sync`).