锁定语句有多昂贵? [英] How expensive is the lock statement?

查看:18
本文介绍了锁定语句有多昂贵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试验多线程和并行处理,我需要一个计数器来对处理速度进行一些基本的计数和统计分析.为了避免并发使用我的类的问题,我在我的类中的私有变量上使用了 lock 语句:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆