C#线程安全快速(est)计数器
如何在C#中获得性能最佳的线程安全计数器 这很简单:C#线程安全快速(est)计数器,c#,multithreading,thread-safety,counter,C#,Multithreading,Thread Safety,Counter,如何在C#中获得性能最佳的线程安全计数器 这很简单: public static long GetNextValue() { long result; lock (LOCK) { result = COUNTER++; } return result; } 但是有更快的替代方案吗?尝试一下这会更简单: return Interlocked.Increment(ref COUNTER); 我建议您在System.Threading库中使用
public static long GetNextValue()
{
long result;
lock (LOCK)
{
result = COUNTER++;
}
return result;
}
但是有更快的替代方案吗?尝试一下这会更简单:
return Interlocked.Increment(ref COUNTER);
我建议您在System.Threading库中使用.NET内置的联锁增量 以下代码将通过引用增加一个长变量,并且是完全线程安全的:
Interlocked.Increment(ref myNum);
来源:根据其他人的建议,
联锁。增量
将比lock()
具有更好的性能。只需查看IL和程序集,您将看到Increment
变成一个“bus lock”语句,其变量直接递增(x86)或“添加”到(x64)
此“bus lock”语句锁定总线,以防止另一个CPU在调用CPU执行其操作时访问总线。现在,看看C#lock()
语句的IL。在这里,您将看到调用Monitor
,以开始或结束一节
换句话说,.Netlock()
语句比.NetInterlocked.Increment
语句做得更多
所以,如果您只想增加一个变量,
Interlock.increment
会更快。查看所有互锁方法,查看可用的各种原子操作,并找到适合您需要的操作。当您想要执行更复杂的操作(如多个相互关联的递增/递减)或序列化对比整数更复杂的资源的访问时,请使用lock()
。如前所述,请使用联锁。递增
MS的代码示例:
以下示例确定生成1000个具有中点值的随机数需要多少个介于0到1000之间的随机数。为了跟踪中点值的数量,将变量midpointCount设置为0,并在随机数生成器每次返回中点值时递增,直到达到10000为止。由于三个线程生成随机数,因此会调用Increment(Int32)方法以确保多个线程不会同时更新中点计数。请注意,锁还用于保护随机数生成器,CountdownEvent对象用于确保Main方法不会在三个线程之前完成执行
using System;
using System.Threading;
public class Example
{
const int LOWERBOUND = 0;
const int UPPERBOUND = 1001;
static Object lockObj = new Object();
static Random rnd = new Random();
static CountdownEvent cte;
static int totalCount = 0;
static int totalMidpoint = 0;
static int midpointCount = 0;
public static void Main()
{
cte = new CountdownEvent(1);
// Start three threads.
for (int ctr = 0; ctr <= 2; ctr++) {
cte.AddCount();
Thread th = new Thread(GenerateNumbers);
th.Name = "Thread" + ctr.ToString();
th.Start();
}
cte.Signal();
cte.Wait();
Console.WriteLine();
Console.WriteLine("Total midpoint values: {0,10:N0} ({1:P3})",
totalMidpoint, totalMidpoint/((double)totalCount));
Console.WriteLine("Total number of values: {0,10:N0}",
totalCount);
}
private static void GenerateNumbers()
{
int midpoint = (UPPERBOUND - LOWERBOUND) / 2;
int value = 0;
int total = 0;
int midpt = 0;
do {
lock (lockObj) {
value = rnd.Next(LOWERBOUND, UPPERBOUND);
}
if (value == midpoint) {
Interlocked.Increment(ref midpointCount);
midpt++;
}
total++;
} while (midpointCount < 10000);
Interlocked.Add(ref totalCount, total);
Interlocked.Add(ref totalMidpoint, midpt);
string s = String.Format("Thread {0}:\n", Thread.CurrentThread.Name) +
String.Format(" Random Numbers: {0:N0}\n", total) +
String.Format(" Midpoint values: {0:N0} ({1:P3})", midpt,
((double) midpt)/total);
Console.WriteLine(s);
cte.Signal();
}
}
// The example displays output like the following:
// Thread Thread2:
// Random Numbers: 2,776,674
// Midpoint values: 2,773 (0.100 %)
// Thread Thread1:
// Random Numbers: 4,876,100
// Midpoint values: 4,873 (0.100 %)
// Thread Thread0:
// Random Numbers: 2,312,310
// Midpoint values: 2,354 (0.102 %)
//
// Total midpoint values: 10,000 (0.100 %)
// Total number of values: 9,965,084
使用系统;
使用系统线程;
公开课范例
{
常量int LOWERBOUND=0;
常量int上限=1001;
静态对象lockObj=新对象();
静态随机rnd=新随机();
静态倒计时事件;
静态整数totalCount=0;
静态整数总和中点=0;
静态int中点计数=0;
公共静态void Main()
{
cte=新的倒计时事件(1);
//开始三个线程。
对于(int ctr=0;ctr-1了解实现细节。确实,锁定比原子操作慢得多,但这与IL无关。如果不是出于语义,这些函数调用将比原子操作快得多,而这不是IL固有的要求。
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
public class Example
{
const int LOWERBOUND = 0;
const int UPPERBOUND = 1001;
static Object lockObj = new Object();
static Random rnd = new Random();
static int totalCount = 0;
static int totalMidpoint = 0;
static int midpointCount = 0;
public static void Main()
{
List<Task> tasks = new List<Task>();
// Start three tasks.
for (int ctr = 0; ctr <= 2; ctr++)
tasks.Add(Task.Run( () => { int midpoint = (UPPERBOUND - LOWERBOUND) / 2;
int value = 0;
int total = 0;
int midpt = 0;
do {
lock (lockObj) {
value = rnd.Next(LOWERBOUND, UPPERBOUND);
}
if (value == midpoint) {
Interlocked.Increment(ref midpointCount);
midpt++;
}
total++;
} while (midpointCount < 50000);
Interlocked.Add(ref totalCount, total);
Interlocked.Add(ref totalMidpoint, midpt);
string s = String.Format("Task {0}:\n", Task.CurrentId) +
String.Format(" Random Numbers: {0:N0}\n", total) +
String.Format(" Midpoint values: {0:N0} ({1:P3})", midpt,
((double) midpt)/total);
Console.WriteLine(s); } ));
Task.WaitAll(tasks.ToArray());
Console.WriteLine();
Console.WriteLine("Total midpoint values: {0,10:N0} ({1:P3})",
totalMidpoint, totalMidpoint/((double)totalCount));
Console.WriteLine("Total number of values: {0,10:N0}",
totalCount);
}
}
// The example displays output like the following:
// Task 3:
// Random Numbers: 10,855,250
// Midpoint values: 10,823 (0.100 %)
// Task 1:
// Random Numbers: 15,243,703
// Midpoint values: 15,110 (0.099 %)
// Task 2:
// Random Numbers: 24,107,425
// Midpoint values: 24,067 (0.100 %)
//
// Total midpoint values: 50,000 (0.100 %)
// Total number of values: 50,206,378