如何昂贵的是锁的语句? [英] How expensive is the lock statement?
本文介绍了如何昂贵的是锁的语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在尝试用多线程和并行处理,我需要一个柜台做一些基本的计算和处理速度的统计分析。为了避免出现同时使用我的班,我在我的课堂上使用的锁声明私有变量:
I've been experimenting with multi threading and parallel processing and I needed a counter to do some basic counting and statistic analysis of the speed of the processing. To avoid problems with concurrent use of my class I've used a lock statement on a private variable in my class:
private object mutex = new object();
public void Count(int amount)
{
lock(mutex)
{
done += amount;
}
}
不过,我不知道......多么昂贵的锁定变量?什么是对性能的负面影响?
But I was wondering... how expensive is locking a variable? What are the negative effects on performance?
推荐答案
下面是的文章的进入成本。简短的回答为50ns。
Here is an article that goes into the cost. Short answer is 50ns.
这篇关于如何昂贵的是锁的语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文