我怎么能写在C#中有条件的锁? [英] How can I write a conditional lock in C#?

查看:156
本文介绍了我怎么能写在C#中有条件的锁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的事情是我一直在使用锁声明以保护我的code的重要组成部分,但现在,我知道我可以允许关键code并发执行的一些条件得到满足。
有没有一种方法来调节锁?

解决方案

 行动doThatThing =的someMethod;

如果(条件)
{
  锁定(thatThing)
  {
     doThatThing();
  }
}
其他
{
  doThatThing();
}
 

The thing is I've been using the lock statement to protect a critical part of my code, but now, I realize I could allow concurrent execution of that critical code is some conditions are met.
Is there a way to condition the lock?

解决方案

Action doThatThing = someMethod;

if (condition)
{
  lock(thatThing)
  {
     doThatThing();
  }
}
else
{
  doThatThing();
}

这篇关于我怎么能写在C#中有条件的锁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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