Monitor.Enter多线程C#中的工作 [英] Monitor.Enter work in multithreading C#

查看:178
本文介绍了Monitor.Enter多线程C#中的工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public static object synLock = new object();
static int x = 10;
public static void Get1()
{
   try
   {
       Monitor.Enter(synLock);
       {
           
           x++;
       }
   }
   catch (Exception exx)
   {
       Console.WriteLine(exx.Message);
   }
   finally
   {
      
   }
}





以上代码我写了 Monitor.Enter(synLock)但不是 Moniter .Exit()在本代码和上面的代码调用中,由两个不同的线程调用,然后是谁负责释放锁定在上面的代码。



in above code I have write Monitor.Enter(synLock)but not Moniter.Exit() in this Code and above code call by two different thread then is it working and who is responsible for releasing lock in above code.

推荐答案

参考

http:/ /stackoverflow.com/questions/15023862/lock-keyword-calls-monitor-enterobject-or-enterobject-boolean [ ^ ]


这篇关于Monitor.Enter多线程C#中的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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