std::thread, mutex, lock_guard, condition_variable, atomics, memory orderings, futures and async.
36 cards · basic cards · AI-written, checked twice. Edit anything.
- What does std::thread represent?
Show answer
A single thread of execution that can run concurrently with other threads.
- How do you create and start a std::thread?
Show answer
Pass a callable (function, lambda, or functor) to the std::thread constructor; the thread starts immediately.
- What does thread.join() do?
Show answer
Blocks the calling thread until the target thread finishes execution.
- What does thread.detach() do?
Show answer
Separates the thread object from the underlying thread, allowing it to continue running independently.
- Can you copy a std::thread object?
Show answer
No, std::thread objects are not copyable; they have a deleted copy constructor and assignment operator.
- What is a race condition?
Show answer
When behavior depends on the relative timing of multiple threads accessing shared data without synchronization.
- What is a mutex?
Show answer
A mutual exclusion lock that ensures only one thread can access a protected resource at a time.
- What does std::lock_guard do?
Show answer
Automatically acquires a mutex upon construction and releases it upon destruction following RAII.
- How does std::unique_lock differ from std::lock_guard?
Show answer
unique_lock allows manual lock and unlock, move semantics, and deferred locking, while lock_guard is simpler and always holds until destruction.
- What is std::shared_lock used for?
Show answer
It allows multiple readers to hold the lock simultaneously while writers get exclusive access in read-write scenarios.
- What is a std::condition_variable?
Show answer
A synchronization primitive that allows threads to wait until a specific condition is signaled by another thread.
- What does condition_variable::wait() do?
Show answer
Releases the held mutex, blocks the thread, and reacquires the mutex upon being notified.
- What is the difference between notify_one() and notify_all()?
Show answer
notify_one() wakes one waiting thread, while notify_all() wakes all waiting threads.
- What is std::atomic?
Show answer
A template class providing atomic, indivisible access to a shared variable without explicit locks.
- What are the main advantages of atomics over mutexes?
Show answer
Lower overhead, lock-free operation potential, no deadlock risk, and better performance for simple shared variables.