原子指令 [英] Atomic Instruction

查看:203
本文介绍了原子指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原子指令是什么意思?

以下内容如何变为原子?

How does the following become Atomic?

TestAndSet

TestAndSet

int TestAndSet(int *x){
   register int temp = *x;
   *x = 1;
   return temp;
}

从软件的角度来看,如果不想使用非阻塞同步原语,那么如何确保指令的原子性?只能在硬件上使用,还是可以使用某些汇编级别的指令优化?

From a software perspective, if one does not want to use non-blocking synchronization primitives, how can one ensure Atomicity of instruction? is it possible only at Hardware or some assembly level directive optimization can be used?

推荐答案

某些机器指令本质上是原子的-例如,在许多体系结构上,读写本地处理器字长正确对齐的值是原子的. >.

Some machine instructions are intrinsically atomic - for example, reading and writing properly aligned values of the native processor word size is atomic on many architectures.

这意味着硬件中断,其他处理器和超线程无法中断读取或存储以及将部分值读取或写入同一位置.

This means that hardware interrupts, other processors and hyper-threads cannot interrupt the read or store and read or write a partial value to the same location.

更复杂的事情,例如原子级的读写,可以通过明确的原子机指令来实现,例如在x86上锁定CMPXCHG.

More complicated things such as reading and writing together atomically can be achieved by explicit atomic machine instructions e.g. LOCK CMPXCHG on x86.

锁定和其他高级构造是基于这些原子基元构建的,它们通常仅保护一个处理器字.

Locking and other high-level constructs are built on these atomic primitives, which typically only guard a single processor word.

仅使用读写指针就可以构建一些巧妙的并发算法,例如在单个阅读者和作家之间共享的链接列表中,或者努力地在多个阅读者和作家之间共享.

Some clever concurrent algorithms can be built using just the reading and writing of pointers e.g. in linked lists shared between a single reader and writer, or with effort, multiple readers and writers.

这篇关于原子指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆