我一直在尝试用多线程和并行处理,我需要一个柜台做一些基本的计算和处理速度的统计分析。为了避免出现同时使用我的班,我在我的课堂上使用的锁声明私有变量:
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.