c#中Monitor的使用
- 首先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的使用的更多相关文章
- C#中Monitor对象与Lock关键字的区别分析
这篇文章主要介绍了C#中Monitor对象与Lock关键字的区别,需要的朋友可以参考下 Monitor对象 1.Monitor.Enter(object)方法是获取 锁,Monitor.Exit(ob ...
- C#知识点总结系列:4、C#中Monitor和Lock以及区别
Monitor对象 1.Monitor.Enter(object)方法是获取锁,Monitor.Exit(object)方法是释放锁,这就是Monitor最常用的两个方法,当然在使用过程中为了避免获取 ...
- C#中Monitor类、Lock关键字和Mutex类
线程:线程是进程的独立执行单元,每一个进程都有一个主线程,除了主线程可以包含其他的线程.多线程的意义:多线程有助于改善程序的总体响应性,提高CPU的效率.多线程的应用程序域是相当不稳定的,因为多个线程 ...
- C#中Monitor和Lock以及区别
Monitor对象 1.Monitor.Enter(object)方法是获取锁,Monitor.Exit(object)方法是释放锁,这就是Monitor最常用的两个方法,当然在使用过程中为了避免获取 ...
- 锁、C#中Monitor和Lock以及区别
1.Monitor.Enter(object)方法是获取锁,Monitor.Exit(object)方法是释放锁,这就是Monitor最常用的两个方法,当然在使用过程中为了避免获取锁之后因为异常,致锁 ...
- 转:C#中Monitor对象与Lock关键字的区别分析
Monitor对象1.Monitor.Enter(object)方法是获取 锁,Monitor.Exit(object)方法是释放锁,这就是Monitor最常用的两个方法,当然在使用过程中为了避免获取 ...
- 关于android sdk中monitor.exe报错的问题
今天又是被坑的一上午.来总结一下: 1. 首先是找不到monitor的问题: 这个可能是一开始环境配置错误.所以我将android sdk重装了一下就好了 2. 第二个是找到monitor.bat发现 ...
- 【C#】【Thread】Monitor和Lock
所谓锁,就是之锁定的区域只能单个线程进入进行操作,其他线程在锁的外围等待.Monitor锁通过Monitor.Enter(obj)和Monitor.Exit(obj)来锁定和解锁.Lock锁则直接Lo ...
- linux中set的用法
功能说明:设置shell.语 法:set [+-abCdefhHklmnpPtuvx]补充说明:用set 命令可以设置各种shell选项或者列 出shell变量.单个选项设置常用的特性.在某些选项之后 ...
随机推荐
- 洋媳妇Susan教育孩子的方法
洋媳妇Susan教育孩子的方法 一个中国婆婆跟我说:「我的儿子去美国留学,毕业后定居美国. 还给我找了个洋媳妇Susan. 如今,小孙子Toby已经3岁了. 今年夏天,儿子為我申请了探亲签证. 在美国 ...
- 图表插件echars的使用案例
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...
- Vim查找替换及正则表达式的使用
原文地址:http://tanqisen.github.io/blog/2013/01/13/vim-search-replace-regex/ 简单替换表达式 :[range]s/from/to/[ ...
- steam linux 使用socks代理
环境:Ubuntu 15.10 64bit,Steam:built:May 10 2016 需要的工具:ssh/shadowsocks等socks5代理,tsocks ---------------- ...
- SQL存储过程基础
什么是存储过程呢?存储过程就是作为可执行对象存放在数据库中的一个或多个SQL命令. 通俗来讲:存储过程其实就是能完成一定操作的一组SQL语句. 那为什么要用存储过程呢?1.存储过程只在创造时进行编译, ...
- 转载自——Json.Net如何在序列化之前修改属性值
今天写一个Json.net比较偏门的用法,也许你很久都用不到,也许你明天就能派上用场. 我们都知道 Json.net 序列话的用法 Test test = new Test() { A = " ...
- HDU 1007 Quoit Design【计算几何/分治/最近点对】
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- Codeforces 739B Alyona and a tree(树上路径倍增及差分)
题目链接 Alyona and a tree 比较考验我思维的一道好题. 首先,做一遍DFS预处理出$t[i][j]$和$d[i][j]$.$t[i][j]$表示从第$i$个节点到离他第$2^{j}$ ...
- Linux下Shell文件内容替换(sed)(转)
sed -i 's/被替换的内容/要替换成的内容/g' file #-i为直接修改并保存 参考: http://blog.sina.com.cn/s/blog_7211cb9201019hgd.htm ...
- 为树莓派(Raspberry pi 2)安装raspbian系统,并用windows自带的远程桌面连接登录
准备工作 树莓派2开发板(保险起见,请装上散热片和风扇): 手机充电器和数据线(输出电压为5V,输出电流为1~2A,电流视开发板上所接附件多少而定): class10 sd小卡,还需要卡架或读卡器: ...