为什么fetch_add使用锁前缀,而fetch_and在boost atomics中使用cmpxchg
why fetch_add uses lock prefix while fetch_and uses cmpxchg in boost atomics
我注意到,在fetch_add
的boost::atomics库x86实现(其中一个不使用编译器内部函数)中,使用了前缀为lock
的add
指令:
static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
{
__asm__ __volatile__
(
"lock; xaddw %0, %1"
: "+q" (v), "+m" (storage)
:
: BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
);
return v;
}
而fetch_and
、fetch_or
和fetch_xor
是通过CAS指令实现的:
#define BOOST_ATOMIC_DETAIL_CAS_LOOP(op, argument, result)
__asm__ __volatile__
(
"xor %%" BOOST_ATOMIC_DETAIL_TEMP_CAS_REGISTER ", %%" BOOST_ATOMIC_DETAIL_TEMP_CAS_REGISTER "nt"
".align 16nt"
"1: movw %[arg], %%dxnt"
op " %%ax, %%dxnt"
"lock; cmpxchgw %%dx, %[storage]nt"
"jne 1b"
: [res] "+a" (result), [storage] "+m" (storage)
: [arg] "q" (argument)
: BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA BOOST_ATOMIC_DETAIL_TEMP_CAS_REGISTER, "memory"
)
static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
{
storage_type res = storage;
BOOST_ATOMIC_DETAIL_CAS_LOOP("andw", v, res);
return res;
}
你知道为什么会这样吗?这是因为您不能在逐位操作中使用锁前缀吗(据我所知,这不是真的)?
fetch_add
使用xadd
指令返回函数效果之前的值。没有xand
或xor
指令,因此使用CAS。
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- boost::进程间消息队列引发错误
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- cmake如何在fedora工作站中找到boost静态库包
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- Boost Graph Library,修复节点大小
- 什么是"#include <boost/functional/hash.hpp> "?
- 基于boost的程序的静态链接——zlib问题
- C++:如何在CLion IDE中安装Boost
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 如何在boost beast http请求中设置http头
- Boost Spirit,获取迭代器内部语义动作
- boost::asio::steady_timer()与sleep()我应该使用哪一个
- boost::asio如何生成多个协同程序,然后加入它们
- 当我尝试使用 sstream 和分面将 Boost Time_duration转换为字符串时,我没有得到所需的格式
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 如何使用boost::具有嵌套结构和最小代码更改的序列化
- 使用Boost Interprocess创建托管共享内存需要很长时间
- Boost::posix_time::ptime舍入到给定的分钟数
- boost xml parsingl将xml的路径作为变量发送