简体   繁体   English

原子地读取非原子变量?

[英]Read a non-atomic variable, atomically?

I have a non-atomic 62-bit double which is incremented in one thread regularly. 我有一个非原子的62位double,它在一个线程中定期增加。 This access does not need to be atomic. 此访问不必是原子的。 However, this variable is occasionally read (not written) by another thread. 但是,此变量有时会被另一个线程读取(而不是写入)。 If I align the variable on a 64-bit boundary the read is atomic. 如果我将变量对准64位边界,则读取是原子的。

However, is there any way I can ensure I do not read the variable mid-way during the increment? 但是,有什么方法可以确保增量过程中不会读取变量? Could I call a CPU instruction which serialises the pipeline or something? 我可以调用一个CPU指令来对管道进行序列化吗? Memory barrier? 记忆障碍?

I thought of declaring the variable atomic and using std::memory_order::memory_order_relaxed in my critical thread (and a stricter memory barrier in the rare thread), but it seems to be just as expensive. 我想到了在我的关键线程中声明变量atomic并使用std::memory_order::memory_order_relaxed (在稀有线程中使用更严格的内存屏障),但它似乎同样昂贵。

Since you tagged x86, this will be x86-specific. 由于标记了x86,因此它将是x86特定的。

An increment is essentially three parts, read, add, write. 增量本质上是三个部分,即读取,添加,写入。 The increment is not atomic, but all three steps (the add doesn't count I suppose, it's not observable anyway) of it are as long as the variable does not cross a cache line boundary (this condition is weaker than having to be aligned to its natural alignment, it has been like this as of P6, before that quadwords had to be aligned). 增量不是原子的,但是所有三个步骤(我想这个加法都不算,无论如何它是不可观察的),只要变量不跨越缓存行边界(此条件比必须对齐要弱)保持自然对齐,直到P6必须对齐为止。

So you already can't read a torn value. 因此,您已经无法读取残缺的值。 The worst you could do is overwrite the variable in between the moments where it is read and the new value is written, but you're only reading it. 您可能做的最坏的事情是在读取变量和写入新值之间覆盖该变量,但您只能读取它。

暂无
暂无

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

相关问题 是否有 function 以原子方式加载非原子值? - Is there a function to load a non-atomic value atomically? 并发读取非原子变量 - Concurrent reads on non-atomic variable 在不锁定的情况下在不同线程中读取(仅)相同的非原子变量是否安全? - Is it safe to read(only) the same non-atomic variable in different threads without locking? std :: memory_order_acq_rel对在其他线程中读取的非原子变量的影响 - Effect of std::memory_order_acq_rel on non-atomic variable read in other thread 并发非原子读/写是未定义的行为吗? - Is Concurrent Non-Atomic Read/Write An Undefined Behavior? 与原子变量相同的高速缓存行的非原子加载会导致原子变量失败吗? - Will a non-atomic load to the same cache line as an atomic variable cause the atomic variable to fail? 通过联合非原子访问原子 - Non-atomic access to atomic through a union 是否安全地解除引用不同线程中原子对象的READ ONLY非原子指针? - Is dereferencing a READ ONLY non-atomic pointer to an atomic object in different threads safe? C++ 线程安全:如果只有一个线程可以写入非原子变量但多个线程从中读取..会遇到问题吗? - C++ Thread Safety: If only one thread can write to a non-atomic variable but multiple threads read from it.. can problems be encountered? 如何在不阻塞的情况下保护非原子变量不被中断和应用程序代码修改 - How to protect a non-atomic variable from modification by both interrupt and application code without blocking
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM