atomic_int和int&#xff1f之间有什么区别;

发布于 2025-02-07 01:30:23 字数 75 浏览 2 评论 0原文

当仅在线程之间的关键部分中写入和读取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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪意 2025-02-14 01:30:23

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 with std::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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文