简体   繁体   English

我可以放置一个 std::atomic<int64> 在共享内存中并期望原子操作?

[英]Can I place a std::atomic<int64> in shared memory and expect atomic operation?

Does std::atomic play well in shared memory, or is it undefined? std::atomic 在共享内存中运行良好,还是未定义? It seems like an easy way to add lockless basic types to shared memory however I could believe that it's not possible to guarantee atomic behaviour in the context of shared memory.将无锁基本类型添加到共享内存似乎是一种简单的方法,但是我相信在共享内存的上下文中不可能保证原子行为。

为什么不呢,你只需要在共享内存区域内正确分配和构造它。

It depends.这取决于。

If the architecture you are using supports atomic operations on 64-bit types, I would expect it to work.如果您使用的架构支持 64 位类型的原子操作,我希望它能够工作。 If std::atomic is simulating atomic operations with mutexes then you will have a problem:如果std::atomic使用互斥体模拟原子操作,那么您将遇到问题:

  • Shared memory is usually used to communicate between processes - and the mutex being used may only work between threads in a single process (for example the Windows CriticalSection API).共享内存通常用于进程之间的通信 - 正在使用的互斥锁可能仅在单个进程中的线程之间工作(例如 Windows CriticalSection API)。
  • Alternatively, shared memory is quite likely to be mapped at different addresses in different processes, and the mutex may have internal pointers which mean that doesn't work.或者,共享内存很可能被映射到不同进程中的不同地址,并且互斥锁可能具有内部指针,这意味着这不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 std :: atomic将一对原子int32视为一个原子int64? - std::atomic treat a pair of atomic int32 as one atomic int64? 我可以使用 C++20 `std::atomic<t> ::wait()` 或 `std::atomic_flag::wait()` 在共享 memory 中?</t> - Can I use C++20 `std::atomic<T>::wait()` or `std::atomic_flag::wait()` in shared memory? 可以使用跨语言平台的 System V 共享内存在 4 字节 int 上实现无锁原子写/一致读操作吗? - Can a lock-free atomic write / consistent read operation be achieved on a 4 byte int using System V shared memory across language platforms? 如何启用 std::experimental::atomic_shared_pointer - How can I enable std::experimental::atomic_shared_pointer 如何打印std :: atomic的值<unsigned int> - How can I print value of std::atomic<unsigned int> 我可以创建一个线程安全的 std::atomic <vector<int> &gt;? - Can I make a thread-safe std::atomic<vector<int>>? 在共享内存上分配原子 - Allocating a atomic on shared memory 对共享内存的原子访问 - Atomic access to shared memory 在没有Volatile,std :: atomic,semaphore,mutex和spinlock的情况下访问共享内存? - Accessing Shared Memory Without Volatile, std::atomic, semaphore, mutex, and spinlock? 为什么TBB无法将`int`转换为`const tbb :: atomic <unsigned int> &`,但std :: atomic可以吗? - Why can't TBB cast `int` to `const tbb::atomic<unsigned int>&`, but std::atomic can?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM