std::atomic_store
时间: 2023-11-09 15:46:54 浏览: 102
std::atomic_store is a function in the C++ standard library that allows you to atomically store a value into an atomic object. It ensures that the operation is performed as a single, indivisible operation, without any intermediate states visible to other threads.
The syntax for std::atomic_store is as follows:
```cpp
void atomic_store(std::atomic<T>* obj, const T& value);
```
Here, `obj` is a pointer to an std::atomic object, and `value` is the new value to be stored.
For example, let's say we have an atomic integer `std::atomic<int> count` and we want to store the value 10 atomically:
```cpp
std::atomic<int> count;
std::atomic_store(&count, 10);
```
In this case, the value 10 would be stored into `count` atomically, ensuring that no other thread can observe intermediate states during the operation.
It's important to note that std::atomic_store only performs the store operation and does not return any value. If you need to retrieve the previous value, you can use std::atomic_exchange instead.
阅读全文