Multithreading Quiz - Languages C++
Free practice quiz: Multithreading (Languages C++). 51 multiple-choice questions with answers and explanations. Play online, no sign-up needed.
This Languages C++ practice quiz has 51 multiple-choice questions on Multithreading, and 51 of them come with an explanation. Try the 5 sample questions below (tap an option to check yourself), then play the full quiz online. No sign-up needed.
Start quiz: 51 questions, freeSample questions
Which C++ standard library header provides classes and functions for multithreading support?
Show answer
Answer: A. <thread>
The `<thread>` header in C++11 and later provides the `std::thread` class, which is the fundamental building block for creating and managing threads.
What is `std::future` used for in C++ multithreading?
Show answer
Answer: B. To represent the result of an asynchronous operation that may not have completed yet.
`std::future` objects are used to retrieve values from asynchronous operations (like those launched by `std::async`) or from promises. The `get()` method of a future will block until the result is available.
What is the primary role of `std::this_thread::sleep_for()`?
Show answer
Answer: B. To pause the current thread for a specified duration.
`std::this_thread::sleep_for()` causes the currently executing thread to sleep (become dormant) for a specified duration. This is often used to limit CPU usage or introduce delays in concurrent operations.
What does the `std::once_flag` object do when used with `std::call_once`?
Show answer
Answer: B. It is a synchronization mechanism to track whether the associated function has already been executed.
The `std::once_flag` is passed to `std::call_once` to ensure that the target function is executed exactly one time across all threads. The `once_flag` internally manages the state to achieve this guarantee.
What is the primary purpose of `std::packaged_task` when it is invoked?
Show answer
Answer: B. To execute the wrapped callable and store its result/exception in a shared state accessible via a future.
When a `std::packaged_task` is invoked (called like a function), it executes the task it wraps. The result (or exception thrown) is then stored in an internal shared state, which can be retrieved by another thread using the `std::future` obtained from `get_future()` on the `packaged_task`.
More questions in this quiz
Answers and explanations are in the full quiz.
- What is the primary purpose of `std::thread::join()`?
- What happens if you call `std::thread::detach()` on a thread?
- What is a race condition in multithreading?
- Which C++ synchronization primitive is used to prevent multiple threads from accessing a shared resource concurrently?
- What is the purpose of `std::lock_guard`?
- What is `std::unique_lock` primarily used for?
- Which C++ concurrency feature allows threads to wait for a specific condition to become true?
- What is a deadlock in the context of multithreading?
- What is the main advantage of using `std::async` over directly creating `std::thread`?
- What is the purpose of `std::promise`?
- What does the `std::call_once` mechanism ensure?
- What is a thread-local storage (TLS) variable?
- Which C++ concurrency primitive is suitable for implementing a producer-consumer pattern?
- What is the potential issue with using `std::mutex` for protecting a shared resource?
- What is the difference between `std::mutex` and `std::recursive_mutex`?
- What is the purpose of `std::scoped_lock` (C++17)?
- What is a task-based parallelism approach in C++?
- What does `std::thread::hardware_concurrency()` typically return?
- Which of the following is an example of a potential deadlock scenario?
- What is the purpose of a `std::atomic` variable?
- When might you choose `std::shared_mutex` over `std::mutex`?
- What is a thread pool?
- What is the purpose of a barrier synchronization primitive?
- Which C++ concurrency primitive is used to signal an event and wake up waiting threads?
- What is the overhead associated with `std::thread` creation?
- What is the function of `std::this_thread::yield()`?
- What is a major challenge when using `std::async` with `std::launch::async`?
- What is a potential pitfall of using `std::thread` without proper synchronization?
- What is `std::packaged_task` used for?
- What is a key difference between `std::lock_guard` and `std::unique_lock` regarding flexibility?
- What is the potential issue with busy-waiting (e.g., using a `while` loop to check a condition without sleeping)?
- What does `std::memory_order` control in atomic operations?
- What is the purpose of `std::packaged_task` and `std::promise` in relation to `std::future`?
- What is the primary difference between `std::thread` and `std::async` in terms of return value handling?
- Which of the following is an example of a fine-grained locking strategy?
- What is the purpose of `std::lock` (C++11) when used with multiple mutexes?
- What is a significant risk of having too many threads in a system?
- What is the role of `std::timed_mutex`?
- What is the primary purpose of `std::once_flag`?
- What is the purpose of the `std::thread::hardware_concurrency()` function?
- What is the difference between `std::lock_guard` and `std::scoped_lock`?
- What does `std::atomic_flag` provide that `std::atomic<bool>` does not directly support for basic operations?
- What is the purpose of `std::promise::set_value()`?
- When using `std::async` with the default launch policy (`std::launch::async | std::launch::deferred`), what determines whether the task runs immediately or lazily?
- What is a key advantage of using `std::async` with `std::launch::deferred`?
- What does it mean for a `std::thread` object to be 'joinable'?