WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C++ · Expert · question 73 of 100

Explain the use of ’std::atomic’ in C++ for lock-free and concurrent programming.?

📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

In C++, the std::atomic class template provides a way to ensure atomic access to a shared variable across multiple threads. It is a thread-safe type that provides operations that are guaranteed to be atomic, meaning they cannot be interrupted by another thread in the middle of the operation. This makes it a powerful tool for concurrent programming, where multiple threads access shared resources simultaneously.

The std::atomic template can be used with a wide range of data types, including integral types (e.g., int, unsigned long), floating-point types (e.g., float, double), and pointer types (e.g., void*). Some of the common operations that can be performed on an atomic object include load, store, exchange, compare-and-swap, fetch-and-add, and fetch-and-sub.

Here is an example of using std::atomic to increment a shared counter by multiple threads:

    #include <atomic>
    #include <iostream>
    #include <thread>
    #include <vector>
    
    int main() {
        std::atomic<int> counter{0};
        std::vector<std::thread> threads;
        
        for (int i = 0; i < 10; ++i) {
            threads.emplace_back([&counter]() {
                for (int j = 0; j < 1000; ++j) {
                    counter.fetch_add(1, std::memory_order_relaxed);
                }
            });
        }
        
        for (auto& t : threads) {
            t.join();
        }
        
        std::cout << "Final counter value: " << counter.load() << std::endl;
        
        return 0;
    }

In this example, we create an std::atomic<int> variable named counter and initialize it to zero. We then create 10 threads that each increment the counter by 1000 using the fetch_add operation, which atomically adds a value to the counter and returns the previous value. Finally, we wait for all the threads to finish and print the final value of the counter.

Note that we use std::memory_order_relaxed as the memory order parameter to fetch_add in this example, which specifies that there are no synchronization or ordering constraints with other operations. Other memory order parameters such as std::memory_order_acquire and std::memory_order_release can be used to enforce ordering or synchronization between threads as needed.

Overall, std::atomic provides a powerful and flexible way to ensure thread-safe access to shared resources in C++, making it an essential tool for concurrent programming.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C++ interview — then scores it.
📞 Practice C++ — free 15 min
📕 Buy this interview preparation book: 100 C++ questions & answers — PDF + EPUB for $5

All 100 C++ questions · All topics