Interlocked.Increment方法的一定数量间隔

Gre*_*582 2 c# .net-core .net-core-2.0

我们有一个并发的多线程程序.如何使样本数每次增加+5个间隔?Interlocked.Increment,是否有间隔超载?我没有看到它列出.

Microsoft Interlocked.Increment方法

// Attempt to make it increase by 5

private int NumberTest;

for (int i = 1; i <= 5; i++)
{
    NumberTest= Interlocked.Increment(ref NumberTest);
}
Run Code Online (Sandbox Code Playgroud)

这是另一个基于它的问题,

C#创建增加1的全局数字

Joh*_*ica 6

我想你想要Interlocked.Add:

添加两个整数并将第一个整数替换为sum,作为原子操作.

int num = 0;
Interlocked.Add(ref num, 5);
Console.WriteLine(num);
Run Code Online (Sandbox Code Playgroud)