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

最新文章

  1. Photon服务器进阶&一个新游戏的出产(一)
  2. jquery.validate.js插件使用
  3. 【Python】pymongo使用
  4. steps animation
  5. HTML Meta标签
  6. Linux traceroute
  7. [CF724B]Batch Sort(暴力,思维)
  8. iOS 应用内付费(IAP)开发步骤
  9. String类中的equals()方法
  10. 使用头文件climits中的符号常量获知整型数据的表数范围---gyy整理
  11. 转载 8天掌握EF的Code First开发之Entity Framework介绍
  12. 【转】265行JavaScript代码的第一人称3D H5游戏Demo
  13. hdu 5433 Xiao Ming climbing(bfs+三维标记)
  14. Android code wiki
  15. EditText默认不显示光标,不可编辑,点击它,进入编辑状态,光标显示
  16. HDU 4424 Conquer a New Region 最大生成树
  17. js实现文本框溢出文字用省略号(...)表示
  18. 日常API之C#百度人脸识别
  19. angular高级篇之transclude使用详解
  20. Discuz论坛URL静态化规则urlrewrite

热门文章

  1. C++虚函数默认实参的注意事项
  2. C++常用字符串分割方法实例汇总
  3. CNN tensorflow text classification CNN文本分类的例子
  4. Codeforces--615B--Longtail Hedgehog(贪心模拟)
  5. CAS配置(2)之主配置
  6. 如何正确从windows系统(自己电脑)远程访问Linux系统(他人电脑)的mysql数据库(图文详解)
  7. 查看SqlServer连接所使用的端口号
  8. C# 获取所有网卡信息
  9. Redmine使用指南
  10. 利用jsonp进行Ajax跨域请求