【问题标题】:Atomic Interlocked.Add taking async func as parameterAtomic Interlocked.Add 以 async func 作为参数
【发布时间】:2016-04-01 18:53:20
【问题描述】:

我有一个案例,我必须以原子方式实现一个加法并且只是为了保存一个语句 - 我做了以下

int result = Interlocked.Add(ref int source, await ComputeAsync(object someObj);

public async Task<int> ComputeAsync(object someObj)
{
  // some operations
  ....
}

上面的 Interlocked.Add 语句有什么错误。我们可以传递一个将 int 作为参数返回到 add stmt 的 await 函数吗?如果有的话会有什么影响?

【问题讨论】:

  • 任何帮助都是非常 赞赏

标签: asynchronous interlocked


【解决方案1】:

只要您了解ComputeAsync 将在调用Add 之前完全完成,该代码就可以正常工作。也就是执行顺序是这样的:

int value = await ComputeAsync(someObj);
int result = Interlocked.Add(ref int source, value);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多