Explore core concepts, key terms, and best practices in C++ multithreading and concurrency with these essential interview-style questions. Learn about thread creation, synchronization, atomic operations, and strategies to write efficient, safe multi-threaded programs in C++.
Which C++ construct introduced in C++11 is primarily used to create a new thread of execution?
Explanation: std::thread is the standard C++11 construct used to create and manage threads. std::mutex is used for locking data, std::atomic provides atomic operations, and std::future is for handling asynchronous results, not for thread creation.
In a program where two threads update the same variable without synchronization, what condition can occur?
Explanation: A race condition happens when multiple threads access shared data unsafely, causing unpredictable outcomes. Deadlock involves threads waiting indefinitely, thread starvation is when a thread never gets CPU time, and thread pooling is a method for reusing threads.
What is the standard way to prevent race conditions when multiple threads access shared variables?
Explanation: std::mutex provides a locking mechanism that ensures only one thread can access a critical section at a time. std::cout is for output, std::vector is a container, and std::algorithm provides generic algorithms but does not handle synchronization.
What is a deadlock in the context of multithreaded C++ programs?
Explanation: A deadlock occurs when threads are stuck waiting on each other’s resources, resulting in no thread progressing. Finishing tasks, thread exits, or having no threads do not describe deadlock conditions.
Which technique helps to avoid deadlocks in a multithreaded C++ application?
Explanation: Acquiring locks in a consistent order prevents circular waiting, which is a key factor in deadlocks. Ignoring shared resources does not resolve potential issues. Outputting debug info and declaring variables static are unrelated to deadlock avoidance.
Which C++ literal is used for declaring thread-local storage variables?
Explanation: thread_local is the correct keyword for thread-specific storage in C++. The other options are either misspellings or non-existent in the C++ standard.
In C++ multithreading, what does 'concurrency' mean?
Explanation: Concurrency refers to handling multiple tasks or threads in overlapping periods, not necessarily simultaneously. Multiplying numbers, printing, or sorting arrays are unrelated to the concept of concurrency.
What is the main difference between concurrency and parallelism in C++?
Explanation: Concurrency is about managing multiple tasks, possibly interleaved, while parallelism means executing tasks at the same time. They are not identical, and the other options confuse their definitions.
Why would a C++ developer use std::atomic for shared variables?
Explanation: std::atomic provides atomic access to variables, ensuring thread safety without locks. It does not affect math speed, memory allocation, or printing debug messages.
What is the purpose of a thread pool in multi-threaded programming?
Explanation: A thread pool allows efficient task management by reusing threads, reducing creation overhead. Storing data, sorting arrays, or creating windows are unrelated to thread pools.
What is std::future mainly used for in C++ multithreading?
Explanation: std::future allows asynchronous retrieval of computation results. It does not create threads, declare constants, or manage thread priority.
Which C++ mutex allows multiple threads to read but only one thread to write?
Explanation: std::shared_mutex enables multi-reader, single-writer scenarios. std::mutex allows only exclusive locking, std::atomic_mutex does not exist, and std::timed_mutex is for timed locking.
What technique is commonly used to write lock-free programs in C++?
Explanation: std::atomic enables lock-free, thread-safe manipulation of variables, boosting performance. Sharing variables without control invites race conditions; static locals and thread pausing don’t offer lock-free guarantees.
Which of these is a best practice for writing multi-threaded C++ programs?
Explanation: Reducing shared resources lowers contention risk and potential synchronization issues. Sharing everything or using global variables increases risk, and avoiding synchronization can result in bugs.
Why is thorough testing important for C++ multithreaded programs?
Explanation: Multithreaded bugs often depend on timing, making them subtle and difficult to detect. Testing is not automatic, errors can still exist, and no testing method instantly removes all bugs.