【发布时间】:2018-12-31 07:47:01
【问题描述】:
我有多个使用 boost 共享内存的进程。写入器进程将写入共享内存中的数组,如下所示:
void push(int32_t val_)
{
int nextIndex = _currentIndex.fetch_add(1,std::memory_order_relaxed);
_buffer[nextIndex] = val_;
}
//val_ is guaranteed to be >=1
//_buffer is an array of int32_t in shared memory initialized to 0
单个阅读器进程将如下所示:
void process()
{
int idx=0;
while(running)
{
int32_t val = _buffer[idx];
if(val)
{
//do some work...
++idx;
}
}
}
根据提升: “该地址范围内的更改会被同样映射相同共享内存对象的其他进程自动看到。”
我的问题是,假设 _buffer 正确对齐, _buffer 可以简单地是 int32_t 的数组,还是绝对有必要将 _buffer 定义为 std::atomic 的数组?在 x86 上写入 int32_t 是原子的,假设对齐是正确的,并且 boost 保证其他进程会看到更新。
CPU 信息:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 24
On-line CPU(s) list: 0-23
Thread(s) per core: 1
Core(s) per socket: 12
Socket(s): 2
NUMA node(s): 2
Vendor ID: GenuineIntel
CPU family: 6
Model: 63
Stepping: 2
CPU MHz: 2596.945
BogoMIPS: 5193.42
Virtualization: VT-x
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 30720K
NUMA node0 CPU(s): 0-5,12-17
NUMA node1 CPU(s): 6-11,18-23
【问题讨论】:
标签: c++ shared-memory atomic