解鎖 C++ 并發(fā)編程的鑰匙:探索 Atomic 變量
最近在用c++搞項(xiàng)目,因?yàn)槎嗑€程要做一個(gè)類似cnt的保護(hù),今天學(xué)習(xí)了c++的原子操作。
探索c++的原子類型
std::atomic 類型是 C++ 提供的一種機(jī)制,用于實(shí)現(xiàn)多線程之間的安全共享數(shù)據(jù)。它通過原子操作來確保對(duì)共享變量的操作是不可分割的。在多線程環(huán)境下,如果沒有適當(dāng)?shù)耐綑C(jī)制,對(duì)共享變量的讀寫可能會(huì)導(dǎo)致競爭條件,進(jìn)而引發(fā)不確定的行為。std::atomic 類型提供了一種解決方案,讓我們能夠以線程安全的方式訪問這些變量。
關(guān)于具體的函數(shù)和詳細(xì)介紹可以訪問這里:https://cplusplus.com/reference/atomic/atomic/?kw=atomic
這里介紹幾個(gè)常用的:
- load 和 store:用于讀取和寫入原子變量的值。
- exchange:交換原子變量的值,并返回之前的值。
- compare_exchange_strong 和 compare_exchange_weak:比較并交換操作,可在特定條件下修改原子變量的值。
- fetch_add 和 fetch_sub:原子地執(zhí)行加法和減法操作,并返回之前的值。
這里原子操作后為什么要返回之前的值呢?
以fetch_add為例,fetch_add是用于對(duì)原子變量進(jìn)行原子性地增加操作。它執(zhí)行一個(gè)原子的加法操作,并返回加法操作之前的原子變量的值。
這種設(shè)計(jì)是基于并發(fā)編程中的常見需求。返回之前的值允許程序員在執(zhí)行加法操作后,獲取加法之前的原始值。這樣做有以下幾個(gè)方面的優(yōu)點(diǎn):
- 原子性操作的完整性:在多線程并發(fā)環(huán)境下,如果需要進(jìn)行原子性的加法操作,同時(shí)又需要獲取加法前的值,fetch_add 的設(shè)計(jì)能夠保證這兩個(gè)操作的原子性。它在單個(gè)原子操作中完成增加操作,并返回增加前的值,避免了在多線程環(huán)境下的競態(tài)條件。
- 避免競態(tài)條件:返回之前的值可以讓程序員在進(jìn)行加法操作之后,檢查原子變量的舊值,并根據(jù)舊值進(jìn)行后續(xù)的操作。這對(duì)于實(shí)現(xiàn)一些特定的同步模式或算法是非常有用的,因?yàn)樗苊饬艘驗(yàn)椴僮鏖g的競爭導(dǎo)致的意外結(jié)果。
舉個(gè)栗子
這里做一個(gè)簡單的線程池,并實(shí)現(xiàn)一個(gè)task,task的任務(wù)就是對(duì)原子變量counter進(jìn)行遞增,最后我們看結(jié)果是否與預(yù)期一致,這里線程池實(shí)現(xiàn)10個(gè)線程,給線程池推送100000個(gè)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;
//注意:這里不會(huì)確保所有任務(wù)已經(jīng)執(zhí)行完畢,僅僅是等待一段時(shí)間以展示結(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í)行:
細(xì)心的小伙伴可能發(fā)現(xiàn)我的代碼直接使用的counter++,這里需要注意,這只是個(gè)簡單的測試代碼,實(shí)際項(xiàng)目中要最好使用counter.fetch_add(1),因?yàn)閏ounter++不保證++是個(gè)原子操作。我在項(xiàng)目中遇到了該問題,最后加出來總會(huì)比預(yù)期值少,后來換成fetch_add后就正常了。