解鎖 C++ 并發編程的鑰匙:探索 Atomic 變量
最近在用c++搞項目,因為多線程要做一個類似cnt的保護,今天學習了c++的原子操作。
探索c++的原子類型
std::atomic 類型是 C++ 提供的一種機制,用于實現多線程之間的安全共享數據。它通過原子操作來確保對共享變量的操作是不可分割的。在多線程環境下,如果沒有適當的同步機制,對共享變量的讀寫可能會導致競爭條件,進而引發不確定的行為。std::atomic 類型提供了一種解決方案,讓我們能夠以線程安全的方式訪問這些變量。
關于具體的函數和詳細介紹可以訪問這里:https://cplusplus.com/reference/atomic/atomic/?kw=atomic
這里介紹幾個常用的:
- load 和 store:用于讀取和寫入原子變量的值。
- exchange:交換原子變量的值,并返回之前的值。
- compare_exchange_strong 和 compare_exchange_weak:比較并交換操作,可在特定條件下修改原子變量的值。
- fetch_add 和 fetch_sub:原子地執行加法和減法操作,并返回之前的值。
這里原子操作后為什么要返回之前的值呢?
以fetch_add為例,fetch_add是用于對原子變量進行原子性地增加操作。它執行一個原子的加法操作,并返回加法操作之前的原子變量的值。
這種設計是基于并發編程中的常見需求。返回之前的值允許程序員在執行加法操作后,獲取加法之前的原始值。這樣做有以下幾個方面的優點:
- 原子性操作的完整性:在多線程并發環境下,如果需要進行原子性的加法操作,同時又需要獲取加法前的值,fetch_add 的設計能夠保證這兩個操作的原子性。它在單個原子操作中完成增加操作,并返回增加前的值,避免了在多線程環境下的競態條件。
- 避免競態條件:返回之前的值可以讓程序員在進行加法操作之后,檢查原子變量的舊值,并根據舊值進行后續的操作。這對于實現一些特定的同步模式或算法是非常有用的,因為它避免了因為操作間的競爭導致的意外結果。
舉個栗子
這里做一個簡單的線程池,并實現一個task,task的任務就是對原子變量counter進行遞增,最后我們看結果是否與預期一致,這里線程池實現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;
//注意:這里不會確保所有任務已經執行完畢,僅僅是等待一段時間以展示結果
std::this_thread::sleep_for(std::chrono::seconds(5));
std::cout << "Final Counter Value: " << counter << std::endl;
return 0;
}
我們預期最后的結果是100000。g++編譯,不要忘記加-lpthread,執行:
細心的小伙伴可能發現我的代碼直接使用的counter++,這里需要注意,這只是個簡單的測試代碼,實際項目中要最好使用counter.fetch_add(1),因為counter++不保證++是個原子操作。我在項目中遇到了該問題,最后加出來總會比預期值少,后來換成fetch_add后就正常了。