简体   繁体   English

__sync_synchronize 有什么作用?

[英]What does __sync_synchronize do?

I saw an answer to a question regarding timing which used __sync_synchronize().我看到了一个关于使用 __sync_synchronize() 的时间问题的答案

  • What does this function do?这个 function 有什么作用?
  • And when is it necessary to be used?什么时候需要使用?

It is a atomic builtin for full memory barrier .它是完整内存屏障的原子内置函数。

No memory operand will be moved across the operation, either forward or backward.不会在整个操作中向前或向后移动内存操作数。 Further, instructions will be issued as necessary to prevent the processor from speculating loads across the operation and from queuing stores after the operation.此外,将根据需要发出指令,以防止处理器推测整个操作的负载以及在操作之后对存储进行排队。

Check details on the link above.检查上面链接的详细信息。

我猜它会强制使用内存栅栏

This builtin issues a full memory barrier.这个内置函数会发出一个完整的内存屏障。

For memory barrier, cpu sorts our instructions, which will improve efficiency generally, but it may cause unexpected results.对于内存屏障,cpu对我们的指令进行排序,一般会提高效率,但可能会导致意想不到的结果。 For example, there may be four registers in the hardware: when you issue an operation command, one register saves your operation instruction (such as read), two registers save parameters (such as address, size), and the last register is a control register.例如,硬件中可能有四个寄存器:当您发出操作命令时,一个寄存器保存您的操作指令(如读取),两个寄存器保存参数(如地址、大小),最后一个寄存器为控制登记。 After all parameters are ready, hardware will read parameters and execute the program, which could look like this:在所有参数准备好后,硬件将读取参数并执行程序,如下所示:

    writereg (dev.register_size,size);
    writereg (dev.register_addr,addr);
    writereg (dev.register_cmd,READ);
    writereg (dev.register_control,GO);

If the last operation code is placed before the others, then that isn't what we expected, so we can put a memory barrier between the first three codes and the last one, to force the CPU to operate the last code after the first three codes:如果最后一个操作码放在其他之前,那么这不是我们所期望的,所以我们可以在前三个代码和最后一个代码之间放置一个内存屏障,强制CPU在前三个代码之后操作最后一个代码代码:

    writereg (dev.register_size,size);
    writereg (dev.register_addr,addr);
    writereg (dev.register_cmd,READ);
    __sync_synchronize();
    writereg (dev.register_control,GO);

It stops queueing writes and caching reads.它停止排队写入和缓存读取。

"volatile" is compiler option to use code to immediately fetch or store a value, but modern CPUs also have multiple cores with memory caches and delayed writes to external memory. “volatile”是使用代码立即获取或存储值的编译器选项,但现代 CPU 也有多个内核,带有 memory 缓存和延迟写入外部 memory。 __sync_synchronize() makes sure all cores see the same values at the same time. __sync_synchronize() 确保所有内核同时看到相同的值。

...
X = new_value; // compiler and/or CPU could update X later when convienent
...
if ( X ) ... // compiler and/or CPU could use previous cached value of X
...

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

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