C++ Threading Fundamentals Quiz Quiz

Enhance your understanding of C++ threading concepts with these beginner-friendly questions, covering thread creation, synchronization mechanisms, and basic usage scenarios. This quiz helps you grasp key elements of multithreading in C++ for safer and more efficient concurrent programming.

  1. Thread Creation Syntax

    Which of the following is the correct way to create a new standard thread that runs a function called foo in C++?

    1. thread::std(foo);
    2. std::thread t(foo);
    3. make_thread(foo)
    4. thread t = foo();

    Explanation: The correct syntax to create a new thread that runs the function foo is std::thread t(foo);. Options like thread::std(foo); and make_thread(foo) are not part of the C++ standard library. Options such as thread t = foo(); attempt to mimic variable initialization but do not use the correct thread type and constructor.

  2. Thread Joining Purpose

    In C++, what does calling join() on a thread object do?

    1. Pauses the thread for a fixed time
    2. Waits for the thread to finish execution
    3. Starts the thread
    4. Terminates the thread immediately

    Explanation: Calling join() on a thread object makes the current thread wait until the associated thread finishes execution. It does not start or terminate the thread, and it does not pause it for a specific duration. Starting a thread is done during construction, and terminating a thread immediately can lead to unsafe behavior which join does not perform.

  3. Data Races Concept

    What is a data race in the context of C++ threading?

    1. Multiple threads running without any shared resources
    2. A thread being starved of CPU time
    3. A thread running faster than another thread
    4. Two threads accessing the same memory location without proper synchronization

    Explanation: A data race occurs when two or more threads access the same memory location concurrently, and at least one of these accesses is a write, without appropriate synchronization. It is not simply about thread speed or CPU starvation, nor does it refer to threads that do not share resources. The term specifically addresses unsafe shared access.

  4. Mutex Role

    What is the primary role of a mutex in C++ multithreading?

    1. To increase thread execution speed
    2. To allocate memory for thread objects
    3. To permanently block threads from execution
    4. To ensure only one thread accesses a critical section at a time

    Explanation: A mutex is used to protect critical sections by allowing only one thread to access a resource or code block at once. It does not make threads run faster, does not block them permanently, and has no role in memory allocation for thread objects. Its function is focused on synchronization and safety.

  5. Lock Guard Usage

    Why would you use std::lock_guard with a std::mutex in C++?

    1. To copy mutex objects between threads
    2. To run multiple threads in sequence
    3. To terminate a thread safely
    4. To automatically lock and unlock the mutex using RAII

    Explanation: std::lock_guard uses the Resource Acquisition Is Initialization (RAII) idiom to lock the mutex on creation and unlock it when it goes out of scope. It is not used to run threads in sequence, terminate them, or to copy mutex objects. This mechanism ensures mutexes are released safely even if an exception occurs.

  6. std::this_thread::sleep_for Purpose

    What does std::this_thread::sleep_for(std::chrono::seconds(2)) do in C++?

    1. Starts a new thread after 2 seconds
    2. Terminates the current thread after 2 seconds
    3. Pauses the current thread for 2 seconds
    4. Sets thread priority for 2 seconds

    Explanation: std::this_thread::sleep_for pauses the execution of the current thread for the specified duration, in this case, 2 seconds. It does not terminate, start a new thread, or adjust thread priority. The other options either misinterpret the function or assign it unrelated actions.

  7. Detached Thread Meaning

    What does it mean when a thread is detached in C++?

    1. The thread executes only while the parent thread is active
    2. The thread will end as soon as main returns
    3. The thread is waiting for user input
    4. The thread runs independently and cannot be joined

    Explanation: A detached thread operates independently without requiring another thread to join it, meaning its resources are released automatically when it finishes. It does not depend on the lifespan of the main thread or parent thread for execution. Waiting for user input is not related to thread detachment.

  8. std::thread::hardware_concurrency Function

    What is the typical use of std::thread::hardware_concurrency() in C++ programs?

    1. To get the memory size of a thread object
    2. To force the use of only a single thread
    3. To schedule threads in real time
    4. To get the number of concurrent threads supported by the hardware

    Explanation: std::thread::hardware_concurrency() returns an estimate of the number of concurrent threads your system hardware can handle. It does not force single-threaded execution, schedule threads in real time, or report memory size of a thread. This information is typically used for optimal thread pool sizing.

  9. std::unique_lock Benefit

    What is one advantage of using std::unique_lock over std::lock_guard in C++ thread synchronization?

    1. std::unique_lock can only be used with a unique pointer
    2. std::unique_lock supports deferred locking and unlocking
    3. std::unique_lock requires less memory
    4. std::unique_lock only works for read-only data

    Explanation: std::unique_lock provides greater flexibility, such as the option to defer locking or manually unlock and re-lock, which std::lock_guard does not support. It does not inherently reduce memory usage, and it does not require working with unique pointers or restrict to read-only data. Its key feature is the more versatile lock management.

  10. Safe Counter Increment Scenario

    Which of the following approaches helps prevent data races when multiple threads increment a shared int counter in C++?

    1. Ignoring the issue as race conditions only occur with write operations
    2. Protecting the counter with std::mutex
    3. Using a for loop without any synchronization
    4. Passing the counter by value to each thread

    Explanation: Using a std::mutex ensures that only one thread can increment the shared counter at a time, preventing data races. Ignoring the issue is incorrect because race conditions can occur with both read and write operations. Simply using a loop or passing by value does not solve race conditions since the underlying shared data is not protected from concurrent access.