atomic_int和int&#xff1f之间有什么区别;
当仅在线程之间的关键部分中写入和读取int时,线程是否安全? 那么Atomic_int和int?之间有什么区别 “ int”不是原子吗?
Isn't it thread safe when only write and read int in critical section between threads?
So What is the difference between atomic_int and int?
Is 'int' not atomically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
std :: atomic_int
由标准保证,完全不参加数据竞赛。您无需通过关键部分保护它。如果您没有
int
的数据竞赛(例如在关键部分中,所有锁定都可以访问可变尊重),那么您将不会使用std: :atomic_int
。您只需要
std :: atomic< t>
,如果您想访问无默默的共享变量或其他同步,则可以在一次独占所有权时给出1个线程。A
std::atomic_int
is guaranteed by the standard not to have data races, at all. You don't need to protect it with critical sections.If you wouldn't have a data race with
int
(such as in a critical section, with a lock that all accesses to the variable respect), then you redundantly wouldn't withstd::atomic_int
either.You only need
std::atomic<T>
if you want to access a shared variable without mutexes or other synchronization to that gives 1 thread at a time exclusive ownership of it.