解鎖 C++ 并發(fā)編程的鑰匙:探索 Atomic 變量
最近在用c++搞項目,因為多線程要做一個類似cnt的保護,今天學(xué)習(xí)了c++的原子操作。

探索c++的原子類型
std::atomic 類型是 C++ 提供的一種機制,用于實現(xiàn)多線程之間的安全共享數(shù)據(jù)。它通過原子操作來確保對共享變量的操作是不可分割的。在多線程環(huán)境下,如果沒有適當(dāng)?shù)耐綑C制,對共享變量的讀寫可能會導(dǎo)致競爭條件,進而引發(fā)不確定的行為。std::atomic 類型提供了一種解決方案,讓我們能夠以線程安全的方式訪問這些變量。
關(guān)于具體的函數(shù)和詳細介紹可以訪問這里:https://cplusplus.com/reference/atomic/atomic/?kw=atomic

這里介紹幾個常用的:
- load 和 store:用于讀取和寫入原子變量的值。
 - exchange:交換原子變量的值,并返回之前的值。
 - compare_exchange_strong 和 compare_exchange_weak:比較并交換操作,可在特定條件下修改原子變量的值。
 - fetch_add 和 fetch_sub:原子地執(zhí)行加法和減法操作,并返回之前的值。
 
這里原子操作后為什么要返回之前的值呢?
以fetch_add為例,fetch_add是用于對原子變量進行原子性地增加操作。它執(zhí)行一個原子的加法操作,并返回加法操作之前的原子變量的值。
這種設(shè)計是基于并發(fā)編程中的常見需求。返回之前的值允許程序員在執(zhí)行加法操作后,獲取加法之前的原始值。這樣做有以下幾個方面的優(yōu)點:
- 原子性操作的完整性:在多線程并發(fā)環(huán)境下,如果需要進行原子性的加法操作,同時又需要獲取加法前的值,fetch_add 的設(shè)計能夠保證這兩個操作的原子性。它在單個原子操作中完成增加操作,并返回增加前的值,避免了在多線程環(huán)境下的競態(tài)條件。
 - 避免競態(tài)條件:返回之前的值可以讓程序員在進行加法操作之后,檢查原子變量的舊值,并根據(jù)舊值進行后續(xù)的操作。這對于實現(xiàn)一些特定的同步模式或算法是非常有用的,因為它避免了因為操作間的競爭導(dǎo)致的意外結(jié)果。
 
舉個栗子
這里做一個簡單的線程池,并實現(xiàn)一個task,task的任務(wù)就是對原子變量counter進行遞增,最后我們看結(jié)果是否與預(yù)期一致,這里線程池實現(xiàn)10個線程,給線程池推送100000個task。
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <queue>
#include <functional>
#include <atomic>
class ThreadPool {
public:
    ThreadPool(size_t numThreads) : stop(false) {
        for (size_t i = 0; i < numThreads; ++i) {
            threads.emplace_back([this] {
                while (true) {
                    std::function<void()> task;
                    {
                        std::unique_lock<std::mutex> lock(queueMutex);
                        condition.wait(lock, [this] { return stop || !tasks.empty(); });
                        if (stop && tasks.empty()) {
                            return;
                        }
                        task = std::move(tasks.front());
                        tasks.pop();
                    }
                    task();
                }
            });
        }
    }
    template <class F>
    void AddTask(F&& f) {
        {
            std::lock_guard<std::mutex> lock(queueMutex);
            tasks.emplace(std::forward<F>(f));
        }
        condition.notify_one();
    }
    ~ThreadPool() {
        {
            std::lock_guard<std::mutex> lock(queueMutex);
            stop = true;
        }
        condition.notify_all();
        for (std::thread& worker : threads) {
            worker.join();
        }
    }
private:
    std::vector<std::thread> threads;
    std::queue<std::function<void()>> tasks;
    std::mutex queueMutex;
    std::condition_variable condition;
    bool stop;
};
int main() {
    std::atomic<int> counter(0);
    ThreadPool pool(10);
    constexpr int numTasks = 100000;
    for (int i = 0; i < numTasks; ++i) {
        pool.AddTask([&counter]() {
            counter++;
        });
    }
    std::cout << "Waiting for tasks to complete..." << std::endl;
    //注意:這里不會確保所有任務(wù)已經(jīng)執(zhí)行完畢,僅僅是等待一段時間以展示結(jié)果
    std::this_thread::sleep_for(std::chrono::seconds(5));
    std::cout << "Final Counter Value: " << counter << std::endl;
    return 0;
}我們預(yù)期最后的結(jié)果是100000。g++編譯,不要忘記加-lpthread,執(zhí)行:

細心的小伙伴可能發(fā)現(xiàn)我的代碼直接使用的counter++,這里需要注意,這只是個簡單的測試代碼,實際項目中要最好使用counter.fetch_add(1),因為counter++不保證++是個原子操作。我在項目中遇到了該問題,最后加出來總會比預(yù)期值少,后來換成fetch_add后就正常了。















 
 
 














 
 
 
 