Interlocked.Exchange 可以交换两个 byte[] 数组吗?
我想原子地交换两个字节数组,而不需要 一把锁。即我不想想
byte[] src;
byte[] dest;
lock(synchLock)
{
dest = src;
}
要这样做 Interlocked.Exchange 可以吗?我在文档中看到它适用于 int 数组。
谢谢!
I want to swap two byte arrays atomically, without the need for
a lock. i.e. I don't want to do
byte[] src;
byte[] dest;
lock(synchLock)
{
dest = src;
}
Is this possible with Interlocked.Exchange ? I see it works for int arrays in docs.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
交换数组引用或交换它们的元素?参考文献 - 是,元素 - 否。没有适用于数组的原子命令。
Swap array references or swap their elements? References - yes, elements - no. There's no atomic command that works with arrays.
目前尚不清楚您在问什么,但是
InterlockedExchange
自动执行以下操作:请注意,该操作仅涉及一个变量以及两个临时变量(正在写入的值和返回的先前值)。而“交换”通常意味着写入两个变量,使得每个变量都具有另一个变量中预先存在的值。那就是:
InterlockedExchange 不能用于实现对两个变量具有原子效果的无锁交换。
It's not clear what you're asking, but
InterlockedExchange
atomically does the following:Note that only one variable is involved in the operation, along with two temporaries (the value being written, and the prior value returned). Whereas "swap" usually means writing two variables, such that each has the value which pre-existed in the other. That would be:
InterlockedExchange
cannot be used to implement lock-less swap with atomic effect on both variables.是的,Interlocked.Exchange 支持所有引用类型和一些选定的值类型(Int32/64/Ptr、Single、Double)。
Yes,
Interlocked.Exchange
supports all reference types and a few selected value types (Int32/64/Ptr, Single, Double).