Footnote: compare_exchange_strong's implementation pseudocode :
bool compare_exchange_strong (T& expected, T desired)
{
if (this->load() == expected) {
this->store(desired);
return true;
}
else {
expected = this->load();
return false;
}
}
The weak form may spuriously fail so that it returns false even when the expected value is present. But the weak form is sometimes more efficient than the strong version.
What does atomic<>::store() mean by default ?
The store() operation performs a so-called release operation on the affected memory location, which by default ensures that all prior memory operations, whether atomic or not, become visible to other threads before the effect of the store operation.
What does atomic<>::load() mean by default ?
The load() operation performs a so-called acquire operation on the affected memory location, which by default ensures that all following memory operations, whether atomic or not, become visible to other threads after the load operation.
2) The low level interfaces
4) The memory order
std::memory_order_seq_cst (sequential consistent memory order, by default)
std::memory_order_release
std::memory_order_acquire
std::memory_order_relaxed
5) Examples
#include <atomic> // for atomics
#include <future> // for async() and futures
#include <thread> // for this_thread
#include <chrono> // for durations
#include <iostream>
long data;
std::atomic<bool> readyFlag(false);
void provider ()
{
std::cout << "<return>" << std::endl;
std::cin.get();
data = 42;
// signal readiness
readyFlag.store(true);
}
void consumer ()
{
// wait for readiness and do something else
while (!readyFlag.load()) {
std::cout.put(’.’).flush();
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
std::cout << "\nvalue : " << data << std::endl;
}
int main()
{
auto p = std::async(std::launch::async,provider);
auto c = std::async(std::launch::async,consumer);
}
Note: always should initialize atomic objects because the default constructor does not fully initialize it (it’s not that the initial value is undefined, it is that the lock is uninitialized).
本文深入探讨了C++中标准库atomic的使用方法,包括强比较交换和弱比较交换的区别,store和load操作的含义及其默认行为,并通过示例展示了如何利用原子变量实现线程间的同步。
6847

被折叠的 条评论
为什么被折叠?



