为什么没有接受双精度作为参数的 Interlocked.Add 的重载? [英] Why is there no overload of Interlocked.Add that accepts Doubles as parameters?

查看:15
本文介绍了为什么没有接受双精度作为参数的 Interlocked.Add 的重载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常欣赏 Threading.Interlocked 类提供的原子性;不过,我不明白为什么 Add 函数只提供两种重载:一种用于整数,另一种用于长整数.为什么不是 Doubles 或任何其他数字类型?

I fully appreciate the atomicity that the Threading.Interlocked class provides; I don't understand, though, why the Add function only offers two overloads: one for Integers, another for Longs. Why not Doubles, or any other numeric type for that matter?

显然,更改 Double 的预期方法是 CompareExchange;我猜这是因为修改 Double 是比修改 Integer 更复杂的操作.我仍然不清楚为什么,如果 CompareExchange 和 Add 都可以接受整数,它们就不能同时接受双精度数.

Clearly, the intended method for changing a Double is CompareExchange; I am GUESSING this is because modifying a Double is a more complex operation than modifying an Integer. Still it isn't clear to me why, if CompareExchange and Add can both accept Integers, they can't also both accept Doubles.

推荐答案

Interlocked 类环绕 Windows API Interlocked** 函数.

The Interlocked class wraps around the Windows API Interlocked** functions.

这些反过来又围绕着原生处理器 API,使用 x86 的 LOCK 指令前缀.它只支持为以下指令添加前缀:

These are, in turn, wrapping around the native processor API, using the LOCK instruction prefix for x86. It only supports prefixing the following instructions:

BT、BTS、BTR、BTC、XCHG、XADD、ADD、OR、ADC、SBB、AND、SUB、XOR、NOT、NEG、INC、DEC

BT, BTS, BTR, BTC, XCHG, XADD, ADD, OR, ADC, SBB, AND, SUB, XOR, NOT, NEG, INC, DEC

您会注意到,这些反过来又几乎映射到互锁方法.不幸的是,此处不支持非整数类型的 ADD 函数.64 位平台支持添加 64 位长.

You'll note that these, in turn, pretty much map to the interlocked methods. Unfortunately, the ADD functions for non-integer types are not supported here. Add for 64bit longs is supported on 64bit platforms.

这是一篇很棒的文章在指令级别讨论锁语义.

这篇关于为什么没有接受双精度作为参数的 Interlocked.Add 的重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆