c# 多线程 --Mutex(互斥锁)
互斥锁(Mutex)
互斥锁是一个互斥的同步对象,意味着同一时间有且仅有一个线程可以获取它。
互斥锁可适用于一个共享资源每次只能被一个线程访问的情况
函数:
//创建一个处于未获取状态的互斥锁
Public Mutex();
//如果owned为true,互斥锁的初始状态就是被主线程所获取,否则处于未获取状态
Public Mutex(bool owned);
如果要获取一个互斥锁。应调用互斥锁上的WaitOne()方法,该方法继承于Thread.WaitHandle类
它处于等到状态直至所调用互斥锁可以被获取,因此该方法将组织住主调线程直到指定的互斥锁可用,如果不需要拥有互斥锁,用ReleaseMutex方法释放,从而使互斥锁可以被另外一个线程所获取。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- namespace MyTTCon
- {
- class shareRes
- {
- public static int count = ;
- public static Mutex mutex = new Mutex();
- }
- class IncThread
- {
- int number;
- public Thread thrd;
- public IncThread(string name, int n)
- {
- thrd = new Thread(this.run);
- number = n;
- thrd.Name = name;
- thrd.Start();
- }
- void run()
- {
- Console.WriteLine(thrd.Name + "正在等待 the mutex");
- //申请
- shareRes.mutex.WaitOne();
- Console.WriteLine(thrd.Name + "申请到 the mutex");
- do
- {
- Thread.Sleep();
- shareRes.count++;
- Console.WriteLine("In " + thrd.Name + "ShareRes.count is " + shareRes.count);
- number--;
- } while (number > );
- Console.WriteLine(thrd.Name + "释放 the nmutex");
- // 释放
- shareRes.mutex.ReleaseMutex();
- }
- }
- class DecThread
- {
- int number;
- public Thread thrd;
- public DecThread(string name, int n)
- {
- thrd = new Thread(this.run);
- number = n;
- thrd.Name = name;
- thrd.Start();
- }
- void run()
- {
- Console.WriteLine(thrd.Name + "正在等待 the mutex");
- //申请
- shareRes.mutex.WaitOne();
- Console.WriteLine(thrd.Name + "申请到 the mutex");
- do
- {
- Thread.Sleep();
- shareRes.count--;
- Console.WriteLine("In " + thrd.Name + "ShareRes.count is " + shareRes.count);
- number--;
- } while (number > );
- Console.WriteLine(thrd.Name + "释放 the nmutex");
- // 释放
- shareRes.mutex.ReleaseMutex();
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- IncThread mthrd1 = new IncThread("IncThread thread ", );
- DecThread mthrd2 = new DecThread("DecThread thread ", );
- mthrd1.thrd.Join();
- mthrd2.thrd.Join();
- }
- }
- }
原文链接:http://www.cnblogs.com/tianzhiliang/archive/2010/09/01/1814822.html
c# 多线程 --Mutex(互斥锁)的更多相关文章
- (转)Linux C 多线程编程----互斥锁与条件变量
转:http://blog.csdn.net/xing_hao/article/details/6626223 一.互斥锁 互斥量从本质上说就是一把锁, 提供对共享资源的保护访问. 1. 初始化: 在 ...
- RWLock——一种细粒度的Mutex互斥锁
RWMutex -- 细粒度的读写锁 我们之前有讲过 Mutex 互斥锁.这是在任何时刻下只允许一个 goroutine 执行的串行化的锁.而现在这个 RWMutex 就是在 Mutex 的基础上进行 ...
- 多线程、互斥锁、异步、GIL
多线程-threading python的thread模块是比较底层的模块,python的threading模块是对thread做了一些包装的,可以更加方便被使用 from threading imp ...
- C++11 多线程同步 互斥锁 条件变量
在多线程程序中,线程同步(多个线程访问一个资源保证顺序)是一个非常重要的问题,Linux下常见的线程同步的方法有下面几种: 互斥锁 条件变量 信号量 这篇博客只介绍互斥量和条件变量的使用. 互斥锁和条 ...
- 多线程之互斥锁(By C++)
首先贴一段win32API实现的多线程的代码,使用CreateThread实现,如果不要传参数,就把第四个参数设为NULL #include<Windows.h> #include< ...
- golang mutex互斥锁分析
互斥锁:没有读锁写锁之分,同一时刻,只能有一个gorutine获取一把锁 数据结构设计: type Mutex struct { state int32 // 将一个32位整数拆分为 当前阻塞的gor ...
- Go 标准库 —— sync.Mutex 互斥锁
Mutex 是一个互斥锁,可以创建为其他结构体的字段:零值为解锁状态.Mutex 类型的锁和线程无关,可以由不同的线程加锁和解锁. 方法 func (*Mutex) Lock func (m *Mut ...
- C# Mutex互斥锁
Mutex 构造函数 (Boolean, String, Boolean) public Mutex ( bool initiallyOwned, string name, out bool crea ...
- C# mutex互斥锁构造
概念 Mutext 出现的比monitor更早,而且传承自COM,当然,waitHandle也是它的父类,它继承了其父类的功能,有趣的是Mutex的脾气非常的古怪,它 允许同一个线程多次重复访问共享区 ...
- c++多线程编程互斥锁初步
上一次讲述了多线程编程,但是由于线程是共享内存空间和资源的,这就导致:在使用多线程的时候,对于共享资源的控制要做的很好.先上程序: #include <iostream> #include ...
随机推荐
- 3.Android 优化布局(解决TextView布局)
转载:http://www.jianshu.com/p/d3027acf475a 今天分享一个Layout布局中的一个小技巧,希望看过之后你也可以写出性能更好的布局,我个人的目的是用最少的view写出 ...
- [学习笔记]lca-倍增
The article is to Jimmy.(方老师讲过了还不会,想怎样???) 求一棵树上两个节点的最近公共祖先有两种算法: (离线); 倍增(在线). 这篇博客只介绍倍增的写法. 表示节点的祖 ...
- NOI2016模拟赛Zbox loves stack
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- [IOS swift对比oc]
http://www.cocoachina.com/industry/20140605/8686.html WWDC 2014上苹果再次惊世骇俗的推出了新的编程语言Swift 雨燕, 这个消息会前没有 ...
- Codeforces 711E ZS and The Birthday Paradox
传送门 time limit per test 2 seconds memory limit per test 256 megabytes input standard input output st ...
- UVa 11998 Broken Keyboard (数组模拟链表问题)
题目链接: 传送门 Broken Keyboard #include<bits/stdc++.h> using namespace std; char str[100010]; int m ...
- 第三次作业——个人作业,k米案例分析
第一部分 调研,评测 评测 1.下载并使用 第一次打开,没什么很深的印象,看见"扫一扫",随手就点了,然后就出现了严重的卡顿,大概是刚启动并且第一次启动的原因,后面就还好了.而且第 ...
- 通过命令行连接Wifi
前提:无线网卡驱动正常安装 1.检查连接无线的接口 $ iwconfig 一般无线接口为wlan0 2.检查无线接口是否工作 $ sudo ip link set wlan0 up 3.扫描周围无线网 ...
- 日志模块logging使用心得
在应用程序使用中,日志输出对应用维护人员.开发人员判断程序的问题起重要作用. 那么在python中如何定义程序的日志输出? 推荐使用日志模块logging 需求:实现日志内容输出在文件中和控制器中 i ...
- js网页如何获取手机屏幕宽度
function a(){"屏幕宽高为:"+screen.width+"*"+screen.height:}其它:网页可见区域宽:document.body.c ...