Explore essential concepts of threads in C++ with these beginner-level questions, covering thread creation, usage, synchronization, and behavior. Perfect for those new to multithreading in C++ and wanting to test their grasp of thread basics, parallel tasks, and related programming practices.
Which header file must you include in a C++ program to use threads like std::thread?
Explanation: The correct header to include for thread support in modern C++ is <thread>. <stdio> is for standard input/output, not threading. <functools> and <tasks> are not valid standard C++ headers, so they will not allow you to use std::thread.
When creating a thread to run a function named taskOne(), how do you pass it to std::thread?
Explanation: You pass the function to the constructor of std::thread by writing std::thread t1(taskOne);. Assigning with = would try to invoke the function instead. The arrows and dots used in the other options are not valid syntax for std::thread initialization.
What is the fundamental benefit of using threads in a C++ program, such as the kitchen example with multiple cooks?
Explanation: Threads allow a program to execute multiple tasks at the same time, making use of multi-core processors. Reducing code size and speeding up compilation are unrelated to threads. Removing syntax errors is not a function of threading.
What does calling t1.join() do when used after starting a thread in C++?
Explanation: The join() function pauses the main thread until the thread represented by t1 completes its task. Stopping, ignoring, or repeating the thread's function are not performed by join(). The other options are incorrect or misleading regarding thread synchronization.
What may happen if you forget to call join() or detach() on a std::thread before main() ends?
Explanation: If a thread is neither joined nor detached before main() ends, the program risks crashing or producing undefined behavior. Threads do not automatically finish; ignoring them can leave the process in a bad state. The paused and no-effect options are incorrect.
What is the result of calling t1.detach() after starting a thread in C++?
Explanation: detach() allows the thread to execute independently, separating it from the main thread's lifetime. The thread is not deleted, nor does it run twice. The main thread does not wait for the detached thread's completion; that's what join() would do.
If you detach a thread running taskOne() that prints 'Cooking rice' and immediately print 'Dinner is ready' in main, which output may appear first?
Explanation: Since the detached thread runs asynchronously, 'Dinner is ready' in main could print first if main finishes before the detached thread. 'Cooking rice' might appear second or potentially not at all if the thread doesn't run in time. Error and no output are incorrect in this expected scenario.
Which of the following is a valid function signature for a thread function in C++ if it takes no parameters and returns nothing?
Explanation: A thread function that returns nothing and takes no parameters should have the signature void taskOne(). The other options either have parameters, return a different type, or omit the return type and are thus unsuitable.
In the code std::thread t1(taskOne);, what does t1 refer to?
Explanation: t1 is the name of the thread object created to manage and control the new thread. It is not the function result, input data, or a file pointer. These distractor options are unrelated to thread objects.
If t1.join() is commented out and main() finishes quickly, what happens to the thread running taskOne()?
Explanation: If main finishes first, the program may end before the thread completes, causing it to be terminated. It is not guaranteed to finish, will not start after main ends, and does not run inside main. Thus, the other options are incorrect for thread lifecycle.
Before the main() function ends in a C++ program using threads, what must be done for each std::thread?
Explanation: To avoid abnormal termination or resource leaks, each std::thread must be either joined or detached before main returns. Deleting thread pointers, calling run(), or setting to null are not valid or necessary procedures for thread management.
If you want to run a function with arguments in a new C++ thread, what is a valid syntax?
Explanation: Arguments are passed after the function in the thread constructor. Assigning with = would call the function instead. The other options use invalid syntax, such as using dot notation or brackets, which do not create threads properly.
Why does calling join() on a thread before printing 'Dinner is ready' ensure correct message order?
Explanation: join() guarantees that the main thread will not print 'Dinner is ready' until the thread task is complete. It does not speed up or block the thread, nor does it cause simultaneous prints. Only waiting for completion is correct here.
In the kitchen example, why does having five cooks illustrate multithreading?
Explanation: Having five cooks demonstrates multiple parallel tasks, which matches how threads allow simultaneous operations. The other options either reference a single worker or unrelated kitchen traits that do not metaphorically reflect threads.
Which of the following is NOT directly achieved by using C++ threads?
Explanation: Threads are about executing tasks in parallel and using resources efficiently, not fixing syntax errors. Running functions in parallel, resource utilization, and asynchronous task execution are all direct benefits, so only syntax errors are unrelated.
In a thread function printing 'Cooking ricen', why is std::endl or n included after the message?
Explanation: Using std::endl or the newline character adds a new line after the printed message for readability. It does not delay output, clear the screen, or end the program. These other options misrepresent the purpose of line-breaks in output.