C++0x atomic template implementation

人盡茶涼 提交于 2019-12-22 04:08:27

问题


I know that similar template exits in Intel's TBB, besides that I can't find any implementation on google or in Boost library.


回答1:


You can find discussions about this feature implementation in boost there : http://lists.boost.org/Archives/boost/2008/11/144803.php

> Can the N2427 - C++ Atomic Types and Operations be implemented

> without the help of the compiler?

No.

They don't need to be intrinsics if you can write inline assembler (or separately-compiled assembler for that matter) then you can write the operations themselves directly. You might even be able to use simple C++ (e.g. just plain assignment for load or store). The reason you need compiler support is preventing inappropriate optimizations: atomic operations can't be optimized out, and generally must not be reordered before or after any other operations. This means that even non-atomic stores performed before an atomic store have to be complete, and can't be cached in a register (for example). Also, loads that occur after an atomic operation cannot be hoisted before the atomic op. On some compilers, just using inline assembler is enough. On others, calling an external function is enough. MSVC provides _ReadWriteBarrier() to provide the compiler ordering. Other compilers need other flags.



来源:https://stackoverflow.com/questions/523827/c0x-atomic-template-implementation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!