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. 85.Ext.Window

    转自:https://chenjumin.iteye.com/blog/668421 1.主要配置项:       closable:是否允许关闭窗口,默认为true.       closeActi ...

  2. 5-7 第五天 微信 JS-SDK-简介

    微信的SDK显然呢并不是在这个公众号里面直接使用的,而是在网页里面使用的.什么样的网页呢?就是微信内置的浏览器.你从朋友圈.从好友消息.从群消息,确定是从公众号的回复里面打开一个链接. 便会启动一个浏 ...

  3. Prime Path(bfs)

    http://poj.org/problem?id=3126 题意:给两个四位数n,m,将n变成m需要多少步,要求每次只能改变n的某一位数,即改变后的数与改变前的数只有一位不同,且每次改变后的数都是素 ...

  4. 原生JS---1

    js的历史 在上个世纪的1995年,当时的网景公司正凭借其Navigator浏览器成为Web时代开启时最著名的第一代互联网公司. 由于网景公司希望能在静态HTML页面上添加一些动态效果,于是叫Bren ...

  5. selenium3 + python3 - alert定位

    一.alert\confirm\prompt弹出框操作主要方法有: text:获取文本值 accept() :点击"确认" dismiss() :点击"取消"或 ...

  6. MSSQL:删除系统作业计划

    use [msdb]declare @job_name varchar(100)set @job_name = N'EveryDayBackup.Subplan_1'--注:jobName为维护计划对 ...

  7. UILabel垂直方向显示(上下的顺序显示)。

    NSString* text = @"一"; NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFont ...

  8. 一个能让cin和scanf 一样快的方法:

    cin慢是有原因的,其实默认的时候,cin与stdin总是保持同步的,也就是说这两种方法可以混用,而不必担心文件指针混乱,同时cout和stdout也一样,两者混用不会输出顺序错乱.正因为这个兼容性的 ...

  9. Android 4.0 Launcher2源码分析——主布局文件(转)

    本文来自http://blog.csdn.net/chenshaoyang0011 Android系统的一大特色是它拥有的桌面通知系统,不同于IOS的桌面管理,Android有一个桌面系统用于管理和展 ...

  10. Android,加载离线Android API文档缓慢问题!

    解决方法:在host文件末添加如下信息! 0.0.0.0 www.googleapis.com 0.0.0.0 www.google.com 0.0.0.0 www.google-analytics. ...