简体   繁体   English

C ++ \\ Cli Interlocked :: Add double-如何实现-参考之前的文章

[英]C++\Cli Interlocked::Add double - how to implement - ref to earlier post

My question is 99% answered in the post referenced below, but I'd like to know how the method is to be called. 我的问题在下面引用的帖子中得到了99%的回答,但是我想知道该方法的调用方式。 Since I cannot comment on the solution provided (too low reputation), I was advised to post my comment as a new question. 由于我无法评论所提供的解决方案(信誉太低),因此建议我发表评论作为新问题。

Original post and answer (by Eugene): Why is there no overload of Interlocked.Add that accepts Doubles as parameters? 原始文章和答案(作者Eugene): 为什么没有接受Doubles作为参数的Interlocked.Add重载?

Question: trying to program Parallel::For loop which requires an Interlocked::Add on doubles, so the example is great. 问题:尝试对Parallel::For循环进行编程,该循环需要Interlocked :: Add加倍,因此示例很好。 But how do I call this function? 但是我怎么称呼这个功能呢?

Say B is to be added to A, both are doubles. 说B要加到A,两者都是双打。

Would the call be A = Add(A, B); 该调用是否为A = Add(A,B); ? If not, what should it be? 如果没有,那应该是什么?

It'd suggest using the "classic" form of lock-free operation: 建议使用“经典”形式的无锁操作:

  • Copy the shared variable to a local variable 将共享变量复制到本地变量
  • Calculate the operation result 计算运算结果
  • InterlockedCompareExchange the result, local variable and shared variable. InterlockedCompareExchange结果,局部变量和共享变量。 If it fails (meaning the shared variable has changed in the meantime), retry the whole operation. 如果失败(这意味着共享变量在此期间已更改),请重试整个操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM