首先lock和Minitor有什么区别呢?
其实lock在IL代码中会被翻译成Monitor。也就是Monitor.Enter(obj)和Monitor.Exit(obj).
lock(obj)
{
}
等价为:
try
{   
      Monitor.Enter(obj)

}
catch()
{}
finally
{
      Monitor.Exit(obj)

}
所以lock能做的,Monitor肯定能做,Monitor能做的,lock不一定能做。那么Monitor额外的功能呢?
1:Monitor.TryEnter(obj,timespan)----timeout之后,就不执行这段代码了。lock可是一直会死等的。

2:还有Monitor.Wait()和Monitor.Pulse()。在lock代码里面如果调用了Monitor.Wait(),会放弃对资源的所有权,让别的线程lock进来。然后别的线程代码里Pulse一下(让原线程进入到等待队列),然后在Wait一下释放资源,这样原线程的就可以继续执行了(代码还堵塞在wait那句话呢)。
也就是说,必须两个或多个线程共同调用Wait和Pulse,把资源的所有权抛来抛去,才不会死锁。

和AutoEvent相似是处理同步关系的,但是AutoEvent是跨进程的,而Monitor是针对线程的。
以下是MSDN的代码示例,调试起来很容易看出来两个函数的作用了,因为尽管是多线程程序,但是是同步操作,所以代码始终是单步执行的。

using System;
using System.Threading;
using System.Collections;

namespace MonitorCS1
{
    class
MonitorSample
    {
       
const int MAX_LOOP_TIME = 100;
       
Queue m_smplQueue;

public MonitorSample()
       
{
           
m_smplQueue = new Queue();
       
}
       
public void FirstThread()
       
{
           
int counter = 0;
           
lock (m_smplQueue)
           
{
               
while (counter < MAX_LOOP_TIME)
               
{
                   
//Wait, if the queue is busy.
                   
Monitor.Wait(m_smplQueue);
                   
//Push one element.
                   
m_smplQueue.Enqueue(counter);
                   
//Release the waiting thread.
                   
Monitor.Pulse(m_smplQueue);

counter++;
               
}
               
int i = 0;
           
}
       
}
       
public void SecondThread()
       
{
           
lock (m_smplQueue)
           
{
               
//Release the waiting thread.
               
Monitor.Pulse(m_smplQueue);
               
//Wait in the loop, while the queue is busy.
               
//Exit on the time-out when the first thread stops.
               
while (Monitor.Wait(m_smplQueue, 50000))
               
{
                   
//Pop the first element.
                   
int counter = (int)m_smplQueue.Dequeue();
                   
//Print the first element.
                   
Console.WriteLine(counter.ToString());
                   
//Release the waiting thread.
                   
Monitor.Pulse(m_smplQueue);
               
}
           
}
       
}
       
//Return the number of queue elements.
       
public int GetQueueCount()
       
{
           
return m_smplQueue.Count;
       
}

static void Main(string[] args)
       
{
           
//Create the MonitorSample object.
           
MonitorSample test = new MonitorSample();
           
//Create the first thread.
           
Thread tFirst = new Thread(new
ThreadStart(test.FirstThread));
           
//Create the second thread.
           
Thread tSecond = new Thread(new
ThreadStart(test.SecondThread));
           
//Start threads.
           
tFirst.Start();
           
tSecond.Start();
           
//wait to the end of the two threads
           
tFirst.Join();
           
tSecond.Join();
           
//Print the number of queue elements.
           
Console.WriteLine("Queue Count = " +
test.GetQueueCount().ToString());
       
}
    }
}

c#中Monitor的使用的更多相关文章

  1. C#中Monitor对象与Lock关键字的区别分析

    这篇文章主要介绍了C#中Monitor对象与Lock关键字的区别,需要的朋友可以参考下 Monitor对象 1.Monitor.Enter(object)方法是获取 锁,Monitor.Exit(ob ...

  2. C#知识点总结系列:4、C#中Monitor和Lock以及区别

    Monitor对象 1.Monitor.Enter(object)方法是获取锁,Monitor.Exit(object)方法是释放锁,这就是Monitor最常用的两个方法,当然在使用过程中为了避免获取 ...

  3. C#中Monitor类、Lock关键字和Mutex类

    线程:线程是进程的独立执行单元,每一个进程都有一个主线程,除了主线程可以包含其他的线程.多线程的意义:多线程有助于改善程序的总体响应性,提高CPU的效率.多线程的应用程序域是相当不稳定的,因为多个线程 ...

  4. C#中Monitor和Lock以及区别

    Monitor对象 1.Monitor.Enter(object)方法是获取锁,Monitor.Exit(object)方法是释放锁,这就是Monitor最常用的两个方法,当然在使用过程中为了避免获取 ...

  5. 锁、C#中Monitor和Lock以及区别

    1.Monitor.Enter(object)方法是获取锁,Monitor.Exit(object)方法是释放锁,这就是Monitor最常用的两个方法,当然在使用过程中为了避免获取锁之后因为异常,致锁 ...

  6. 转:C#中Monitor对象与Lock关键字的区别分析

    Monitor对象1.Monitor.Enter(object)方法是获取 锁,Monitor.Exit(object)方法是释放锁,这就是Monitor最常用的两个方法,当然在使用过程中为了避免获取 ...

  7. 关于android sdk中monitor.exe报错的问题

    今天又是被坑的一上午.来总结一下: 1. 首先是找不到monitor的问题: 这个可能是一开始环境配置错误.所以我将android sdk重装了一下就好了 2. 第二个是找到monitor.bat发现 ...

  8. 【C#】【Thread】Monitor和Lock

    所谓锁,就是之锁定的区域只能单个线程进入进行操作,其他线程在锁的外围等待.Monitor锁通过Monitor.Enter(obj)和Monitor.Exit(obj)来锁定和解锁.Lock锁则直接Lo ...

  9. linux中set的用法

    功能说明:设置shell.语 法:set [+-abCdefhHklmnpPtuvx]补充说明:用set 命令可以设置各种shell选项或者列 出shell变量.单个选项设置常用的特性.在某些选项之后 ...

随机推荐

  1. Master of Sequence

    Master of Sequence 时间限制: 10 Sec  内存限制: 128 MB 题目描述 There are two sequences a1,a2,...,an , b1,b2,..., ...

  2. 粟粟的书架(bzoj 1926)

    Description 幸福幼儿园 B29 班的粟粟是一个聪明机灵.乖巧可爱的小朋友,她的爱好是画画和读书,尤其喜欢 Thomas H. Co rmen 的文章.粟粟家中有一个 R行C 列的巨型书架, ...

  3. Bzoj2882 工艺 [线性算法]

    后缀自动机题解 -> http://www.cnblogs.com/SilverNebula/p/6420601.html 后缀自动机敲完,看了下排行,wc为什么别人跑得这么快?……是诶,这最小 ...

  4. CentOS下Samba使用

    1. 软件 Samba需要以下三个基本软件包,相关依赖包未列出 samba: The Samba SMB server samba-client: Samba (SMB) client program ...

  5. VIM使用技巧3

    假如有如下代码: var  foo = "method("+argument1+","+argument2+")" 任务:在每个“+”前后各 ...

  6. hdu 2136(质数筛选+整数分解)

    Largest prime factor Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  7. 一、Ubuntu 简介

    Ubuntu 是一个Linux 系统 Apt-Get apt-get 命令是一个强大的命令行工具,用于同 Ubuntu 的 Advanced Packaging Tool (APT) 一起执行诸如安装 ...

  8. 用chardet判断字符编码的方法

    转自http://www.cnblogs.com/xiaowuyi/archive/2012/03/09/2387173.html 用chardet判断字符编码的方法   1.chardet下载与安装 ...

  9. 使用Fiddle监听HTTPS网页

    HTTPS相对于HTTP增加了安全性,但是仍然不能有效的防止中间人攻击(Man-in-the-MiddleAttack,简称“MITM攻击”) 这就使得Fiddle工具能够有效的监听HTTPS流量 一 ...

  10. webpack学习(一)安装和命令行、一次js/css的打包体验及不同版本错误

    一.前言 找了一个视频教程开始学习webpack,跟着视频学习,在自己的实际操作中发现,出现了很多问题.基本上都是因为版本的原因而导致,自己看的视频是基于webpack 1.x版,而自己现在早已是we ...