简体   繁体   English

使用QAtomicInt作为内存围栏

[英]Using QAtomicInt as memory fence

Again something about lock free... 再次关于无锁...

Suppose I implement a simple array based circular FIFO of integers. 假设我实现了一个基于整数的简单数组循环FIFO。 The FIFO is accessed by 2 threads single producer, single consumer. FIFO由2个线程(单个生产者,单个消费者)访问。 The read and write indexes are always updated AFTER the item is accessed. 读写索引始终在访问该项目后更新。 On a single CPU this would be safe ( assuming the update operation of the indexes is atomic ), since only one thread has write access to an index. 在单个CPU上,这是安全的(假设索引的更新操作是原子的),因为只有一个线程对索引具有写访问权限。 On an SMP machine this may cause problems due to the fact that both CPU's might see a different representation of the index due to its cache and reordering. 在SMP机器上,这可能会导致问题,原因是两个CPU都可能由于其缓存和重新排序而看到索引的不同表示形式。

QT has cross platform atomic variables which also implement a memory fence, would the use of an QAtomicInt make the fifo implementation SMP safe or am I overlooking something ? QT具有跨平台原子变量,该变量也实现了内存隔离,使用QAtomicInt可以使fifo实现SMP安全还是我忽略了什么?

It would be safe, provided the rest of your implementation is sound. 只要您的其余实施都合理,这将是安全的。

As you mention, QAtomicInt implements memory fences that prevent instruction re-ordering, and this also enforces cache coherence between processors (in fact the caches of x86 and x86_64 processors are all coherent anyway, though the coherence protocol can cause pipeline stalls and reduce performance if you're relying on it in a tight loop). 正如您所提到的,QAtomicInt实现了内存围栏 ,以防止指令重新排序,并且这还增强了处理器之间的缓存一致性(实际上x86和x86_64处理器的缓存都是一致的,尽管如果一致性协议会导致流水线停顿并降低性能,您会紧紧地依靠它)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM