Getting Started with C++ Threading: An Easy Quiz Quiz

  1. C++ Thread Creation Basics

    Which header file must you include in your C++ program to use std::thread for creating new threads?

    1. <thread>
    2. <threads>
    3. <pthread>
    4. <concurrent>
    5. <worker>
  2. Starting a Thread

    Given the following code snippet, what is the correct way to start a new thread that executes the function named 'task'? Example: ______;

    1. std::thread t1(task);
    2. thread t1(task);
    3. std::thread::t1(task);
    4. start_thread(task);
    5. run_thread t1(task);
  3. Thread Synchronization

    Which C++ feature is commonly used to prevent multiple threads from accessing shared data at the same time, ensuring safe access?

    1. std::mutex
    2. std::unsafe
    3. std::protect
    4. lock_safe
    5. thread_guard
  4. Ending a Thread

    What must you call on a std::thread object called 't' to wait for the thread to finish before continuing execution?

    1. t.join();
    2. t.run();
    3. t.stop();
    4. t.shutdown();
    5. t.detach();
  5. Detached Threads

    What happens if you call the detach() member function on a std::thread?

    1. The thread runs independently and cannot be joined.
    2. The thread is immediately stopped.
    3. The main function waits for the thread to finish.
    4. The thread is deleted from memory.
    5. The thread joins automatically at program exit.