Last Updated: December 6, 2025
30 quizzes
Which statement about std::thread in C++ is TRUE?
What happens if a joinable std::thread object is destroyed?
How are arguments passed to a function run by std::thread?
Which use of detach() is MOST appropriate?
Which is the BEST way to protect shared data from race conditions?
What is a key advantage of std::lock_guard over manual mutex lock()/unlock()?
Which statement about std::unique_lock is TRUE?
What does std::condition_variable::wait(lock, pred) guarantee?
Which launch policy forces std::async to run the function on a new thread?
Which statement about thread_local variables is CORRECT?
Start a new thread that runs the function worker with an integer argument
std::thread t( worker, );Click an option to fill the blank:
Use RAII to lock a mutex inside a function
std::lock_guard<std::mutex> guard( );Click an option to fill the blank:
Declare an atomic counter of type long
std::<long> counter{0};Click an option to fill the blank:
What is the output of this code involving two threads incrementing a shared int without synchronization?
1#include <iostream>
2#include <thread>
3
4int counter = 0;
5
6void inc() {
7 for (int i = 0; i < 1000; ++i) {
8 ++counter;
9 }
10}
11
12int main() {
13 std::thread t1(inc);
14 std::thread t2(inc);
15 t1.join();
16 t2.join();
17 std::cout << counter << std::endl;
18 return 0;
19}What does this program print about joinable state?
1#include <iostream>
2#include <thread>
3
4void work() {}
5
6int main() {
7 std::thread t(work);
8 std::cout << std::boolalpha << t.joinable() << " ";
9 t.join();
10 std::cout << t.joinable() << std::endl;
11 return 0;
12}What does this code using std::async print?
1#include <iostream>
2#include <future>
3#include <thread>
4
5int compute(int x) {
6 std::this_thread::sleep_for(std::chrono::milliseconds(10));
7 return x * 2;
8}
9
10int main() {
11 auto fut = std::async(std::launch::async, compute, 5);
12 std::cout << "waiting";
13 int result = fut.get();
14 std::cout << " " << result << std::endl;
15 return 0;
16}What is the output regarding thread_local variable values?
1#include <iostream>
2#include <thread>
3
4thread_local int x = 0;
5
6void f() {
7 ++x;
8 std::cout << x << " ";
9}
10
11int main() {
12 std::thread t1(f);
13 std::thread t2(f);
14 t1.join();
15 t2.join();
16 f();
17 std::cout << std::endl;
18 return 0;
19}What does this atomic counter example print?
1#include <iostream>
2#include <atomic>
3#include <thread>
4
5std::atomic<int> counter{0};
6
7void inc() {
8 for (int i = 0; i < 1000; ++i) {
9 counter.fetch_add(1, std::memory_order_relaxed);
10 }
11}
12
13int main() {
14 std::thread t1(inc);
15 std::thread t2(inc);
16 t1.join();
17 t2.join();
18 std::cout << counter.load() << std::endl;
19 return 0;
20}Find the bug in this code using std::thread
Click on the line(s) that contain the bug.
#include <thread> void work(); void start() { std::thread t(work); // do something} // end of function Find the bug in this producer-consumer using condition_variable
Click on the line(s) that contain the bug.
#include <mutex>#include <condition_variable>#include <queue> std::mutex mtx;std::condition_variable cv;std::queue<int> q; void consumer() { std::unique_lock<std::mutex> lock(mtx); if (q.empty()) { cv.wait(lock); } int v = q.front(); q.pop();} Click the line that can cause a deadlock
Click on the line to select.
#include <mutex> std::mutex m1;std::mutex m2; void func1() { std::lock_guard<std::mutex> lock1(m1); std::lock_guard<std::mutex> lock2(m2);} void func2() { std::lock_guard<std::mutex> lock2(m2); std::lock_guard<std::mutex> lock1(m1);} Click the line where the shared data is NOT protected by the mutex
Click on the line to select.
#include <mutex>#include <vector> std::mutex mtx;std::vector<int> data; void add(int x) { std::lock_guard<std::mutex> lock(mtx); data.push_back(x);} int get_first() { if (data.empty()) return -1; return data[0];} Complete the correct use of std::ref to pass a reference to a thread
#include <thread>#include <vector>void append(std::vector<int>& v) { v.push_back(1);}int main() { std::vector<int> v; std::thread t(append, (v)); t.join();}Click an option to fill blank 1:
Fill in the blanks to correctly use condition_variable with a predicate
#include <mutex>#include <condition_variable>std::mutex mtx;std::condition_variable cv;bool ready = false;void wait_for_ready() { std::unique_lock<std::mutex> lock(mtx); cv.(lock, [] { return ; });}Click an option to fill blank 1:
Complete the code to declare and use a future returned from std::async
#include <future>int compute() { return 7; }int main() { auto fut = std::async(std::launch::async, compute); int value = fut.(); << value << std::endl;}Click an option to fill blank 1:
Fill in the blanks to declare a thread_local variable and increment it
#include <thread>#include <iostream> int counter = 0;void inc() { ++counter; std::cout << counter << std::endl;}Click an option to fill blank 1:
Match each concurrency primitive to its main use
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Match async mechanisms with how results are obtained
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Order the typical steps to safely consume items from a shared queue using a mutex and condition_variable
Drag and drop to reorder, or use the arrows.
Order the steps to use std::promise and std::future to compute a value in a background thread
Drag and drop to reorder, or use the arrows.