当前位置:网站首页>std::atomic_flag的test_and_set函数理解
std::atomic_flag的test_and_set函数理解
2022-08-09 17:24:00 【hellozhengyuan】
std::atomic_flag test_and_set函数理解
std::atomic_flag的test_and_set函数原型如下:
bool test_and_set(std::memory_order order = std::memory_order_seq_cst) volatile noexcept; (1) (since C++11)
bool test_and_set(std::memory_order order = std::memory_order_seq_cst) noexcept; (2) (since C++11)
Atomically changes the state of a std::atomic_flag to set (true) and returns the value it held before.
atomic_flag只能有3个状态:
- 未设置(定义时未初始化,在c++20以后在定义时自动初始化为false,即在c++20以后此状态不再存在)
- 清除(false)
- 设置(true)
test_and_set函数的理解
此函数有两种语义:
- 1. test表示先测试(读取当前atomic_flag的值)并返回个这结果值;
- 2. set表示将atomic_flag状态设置为ture。
可以看出,test_and_set函数的返回值与set的结果没有关系,返回值只表示调用test_and_set函数前的atomic_flag当前的状态。调用此函数后atomic_flag状态一定为true。
可以看出atomic_flag原子变量的操作十分的有限。直到c++20以后才新增test()函数,对atomic_flag状态无修改的只读访问函数。
边栏推荐
猜你喜欢
随机推荐
EPIC是什么平台?
混动产品助力,自主SUV市场格局迎来新篇章
一文深入了解 Hybrid 的实现原理
如何在 PC 机上测试移动端的网页?
Cortex-A7 MPCore Architecture
Prometheus full installation
The principle implementation of handwritten flexible.js, I finally understand the multi-terminal adaptation of the mobile terminal
How to play with container local storage through open-local? | Dragon Lizard Technology
A carnival of art and technology, cloud XR supports Anaya 2022 Sandbox Immersive Art Season
动态RDLC报表(七)
What are some good open source automation testing frameworks to recommend?
虚拟补丁备忘单
C语言知识补充
shared usage in d
华为发布「国产Copilot内核」PanGu-Coder,而且真的能用中文哦!
[极客大挑战 2019]HardSQL
动态RDLC报表(一)
win10 uwp 改变鼠标
d中简单禁止垃集
偷偷盘点一下各大互联网公司的实习薪资