On the Intel type of x86 processors including AMD, increasingly there are more CPU cores or processors running in parallel.

In the old days when there was a single processor, the operation:

++i;

Would be thread safe because it was one machine instruction on a single processor. These days laptops have numerous CPU cores so that even single instruction operations aren't safe. What do you do? Do you need to wrap all operations in a mutex or semaphore? Well, maybe you don't need too.

Fortunately, the x86 has an instruction prefix that allows a few memory referencing instruction to execute on specific memory locations exclusively.

There are a few basic structures that can use this:

(for the GNU Compiler)

void atom_inc(volatile int *num)

{

__asm__ __volatile__ ( "lock incl %0" : "=m" (*num));

}

void atom_dec(volatile int *num)

{

__asm__ __volatile__ ( "lock decl %0" : "=m" (*num));

}

int atom_xchg(volatile int *m, int inval)

{

register int val = inval;

__asm__ __volatile__ ( "lock xchg %1,%0" : "=m" (*m), "=r" (val) : "1" (inval));

return val;

}

void atom_add(volatile int *m, int inval)

{

register int val = inval;

__asm__ __volatile__ ( "lock add %1,%0" : "=m" (*m), "=r" (val) : "1" (inval));

}

void atom_sub(volatile int *m, int inval)

{

register int val = inval;

__asm__ __volatile__ ( "lock sub %1,%0" : "=m" (*m), "=r" (val) : "1" (inval));

}

 

For the Microsoft Compiler:

 

void atom_inc(volatile int *num)

{

_asm

{

mov esi, num

lock inc DWORD PTR [esi]

};

}

void atom_dec(volatile int *num)

{

_asm

{ mov esi, num

lock dec DWORD PTR [esi]

};

}

int atom_xchg(volatile int *m, int inval)

{

_asm

{

mov eax, inval

mov esi, m

lock xchg eax, DWORD PTR [esi]

mov inval, eax

}

return inval;

}

void atom_add(volatile int *num, int val)

{

_asm

{ mov esi, num

mov eax, val

lock add DWORD PTR [esi], eax

};

}

void atom_sub(volatile int *num, int val)

{

_asm

{ mov esi, num

mov eax, val

lock sub DWORD PTR [esi], eax

};

}

 

The lock prefix is not universally applied. It only works if all accesses to the locations also use lock. So, even though you use "lock" in one section of code, another section of code that just sets the value will not be locked out. Think of it as just a mutex.

Basic usage:

 

class poll

{

int m_pollCount;

....

....

 

void pollAdd()

{

atom_inc(&m_pollCount);

}

};

The above example increments a poll object count by one.

SRC=http://www.mohawksoft.org/?q=node/78

Atomic operations on the x86 processors的更多相关文章

  1. Voting and Shuffling to Optimize Atomic Operations

    2iSome years ago I started work on my first CUDA implementation of the Multiparticle Collision Dynam ...

  2. 什么是Java中的原子操作( atomic operations)

    1.啥是java的原子性 原子性:即一个操作或者多个操作 要么全部执行并且执行的过程不会被任何因素打断,要么就都不执行. 一个很经典的例子就是银行账户转账问题: 比如从账户A向账户B转1000元,那么 ...

  3. 【转】ARM vs X86 – Key differences explained!

    原文:http://www.androidauthority.com/arm-vs-x86-key-differences-explained-568718/ Android supports 3 d ...

  4. 原子操作(atomic operation)

    深入分析Volatile的实现原理 引言 在多线程并发编程中synchronized和Volatile都扮演着重要的角色,Volatile是轻量级的synchronized,它在多处理器开发中保证了共 ...

  5. A multiprocessing system including an apparatus for optimizing spin-lock operations

    A multiprocessing system having a plurality of processing nodes interconnected by an interconnect ne ...

  6. Adaptively handling remote atomic execution based upon contention prediction

    In one embodiment, a method includes receiving an instruction for decoding in a processor core and d ...

  7. Method and apparatus for an atomic operation in a parallel computing environment

    A method and apparatus for a atomic operation is described. A method comprises receiving a first pro ...

  8. Moving x86 assembly to 64-bit (x86-64)

    While 64-bit x86 processors have now been on the market for more than 5 years, software support is o ...

  9. A trip through the Graphics Pipeline 2011_13 Compute Shaders, UAV, atomic, structured buffer

    Welcome back to what’s going to be the last “official” part of this series – I’ll do more GPU-relate ...

随机推荐

  1. android NDK 神经网络API——是给tensorflow lite调用的底层API,应用开发者使用tensorflow lite即可

    eural Networks API In this document show more Understanding the Neural Networks API Runtime Neural N ...

  2. ReverseEngineerCodeFirst 自定义模板

    1.在你要生成的项目里面在根目录下面添加CodeTemplates文件夹,并在该文件夹下面创建子文件夹ReverseEngineerCodeFirst 2.在ReverseEngineerCodeFi ...

  3. matplotlib之pyplot 知识点滴

    以下是一些常用地址链接,请参考 matplotlib 官方网址 plt.plot()函数细节 Matplotlib 中文用户指南 4.6 编写数学表达式 Python seaborn matplotl ...

  4. Linux通信之poll机制分析

    poll机制分析 韦东山 2009.12.10 所有的系统调用,基于都可以在它的名字前加上“sys_”前缀,这就是它在内核中对应的函数.比如系统调用open.read.write.poll,与之对应的 ...

  5. 【Linux】swap分区简介及空间增加方式

    swap分区简介 Swap分区在系统的物理内存不够用的时候,把硬盘空间中的一部分空间释放出来,以供当前运行的程序使用.那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被释放的空间被临时保存 ...

  6. 设置Hadoop的 dataNode的单个Map的内存配置

    1.进入hadoop的配置目录 ,找到 环境变量的 $HADOOP_HOME cd $HADOOP_HOME 2.修改dataNode 节点的 单个map的能使用的内存配置 找到配置的文件: /opt ...

  7. SQL Server对数据进行删除

    SQL Server对数据进行删除,把页面的信息从数据库删除. auto"> <tr style="background:red"> <td> ...

  8. 时序分析:KMP算法用于序列识别

    考研基础资料之一的<算法与数据结构>,KMP算法作为串匹配的基本算法,为必考题目之一.对于算法入门来说,也是复杂度稍高的一个基本算法. KMP算法作为串匹配的非暴力算法,是为了减少回溯而设 ...

  9. 08--MOOC--C/C++ 根据年月日计算星期几

    计算任何一天是星期几的几种算法 一:常用公式 W = [Y-1] + [(Y-1)/4] - [(Y-1)/100] + [(Y-1)/400] + D Y是年份数,D是这一天在这一年中的累积天数,也 ...

  10. sql server 查询数据判断为空

    and xxx is NOT null and xxx is null