模板类std::atomic<T>介绍

本文介绍了C++中的原子类型std::atomic及其在多线程环境中的作用,防止数据竞争。当不同线程访问共享数据时,非原子操作可能导致数据竞争,产生未定义行为。使用std::atomic可以确保并发访问的正确性,提供内存同步和访问顺序。通过示例展示了如何使用std::atomic避免线程间的不确定性。
摘要由CSDN通过智能技术生成

  头文件#include<atomic>,使用方法atomic<int> val;
  原子类型对象的主要特点是从不同线程访问共享数据,不会导致数据竞争(data race)。数据竞争,简单而言就是,假设线程th1对共享数据进行了修改,而当线程th2对该共享数据进行访问时,其结果是未知的,可能是修改之前的值,也有可能是修改之后的值。

Threads and data races

  A thread of execution is a flow of control within a program that begins with the invocation of a top-level function by std::thread::thread, std::async, or other means.
  Any thread can potentially access any object in the program (objects with automatic and thread-local storage duration may still be accessed by another thread through a pointer or by reference).
  Different threads of execution are always allowed to access (read and modify) different memory locations concurrently, with no interference and no synchronization requirements.
  When an evaluation of an expression writes to a memory location and another evaluation reads or modifies the same memory location, the expressions are said to conflict. A program that has two conflicting evaluations has a data race unless

  • both evaluations execute on the same thread or in the same signal handler, or
  • both conflicting evaluations are atomic operations (see std::atomic), or
  • one of the conflicting evaluations happens-before another (see std::memory_order)

  If a data race occurs, the behavior of the program is undefined.
  (In particular, release of a std::mutex is synchronized-with, and therefore, happens-before acquisition of the same mutex by another thread, which makes it possible to use mutex locks to guard against data races.)

int cnt = 0;
auto f = [&]{cnt++;};
std::thread t1{f}, t2{f}, t3{f}; // undefined behavior
std::atomic<int> cnt{0};
auto f = [&]{cnt++;};
std::thread t1{f}, t2{f}, t3{f}; // OK

Introduction from cppreference.com

  Each instantiation and full specialization of the std::atomic template defines an atomic type. If one thread writes to an atomic object while another thread reads from it, the behavior is well-defined (see memory model for details on data races).
  In addition, accesses to atomic objects may establish inter-thread synchronization and order non-atomic memory accesses as specified by std::memory_order.
  std::atomic is neither copyable nor movable.
在这里插入图片描述

Introduction from cplusplus.com

  Objects of atomic types contain a value of a particular type (T).
  The main characteristic of atomic objects is that access to this contained value from different threads cannot cause data races (i.e., doing that is well-defined behavior, with accesses properly sequenced). Generally, for all other objects, the possibility of causing a data race for accessing the same object concurrently qualifies the operation as undefined behavior.
  Additionally, atomic objects have the ability to synchronize access to other non-atomic objects in their threads by specifying different memory orders.

参考

  1. https://en.cppreference.com/w/cpp/atomic/atomic
  2. http://www.cplusplus.com/reference/atomic/atomic/?kw=atomic

更新中…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YMWM_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值