c# 线程信号量 Mutex
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();
Console.ReadKey();
}
}
}
c# 线程信号量 Mutex的更多相关文章
- 四十三、Linux 线程——线程同步之线程信号量
43.1 信号量 43.1.1 信号量介绍 信号量从本质上是一个非负整数计数器,是共享资源的数目,通常被用来控制对共享资源的访问 信号量可以实现线程的同步和互斥 通过 sem_post() 和 sem ...
- UDP、线程、mutex锁(day15)
一.基于UDP的网络编程模型 服务器端 .创建socket. .将fd和服务器的ip地址和端口号绑定 .recvfrom阻塞等待接收客户端数据 .业务处理 .响应客户端 客户端: .创建socket ...
- 【.NET深呼吸】线程信号量(Semaphore)
Semaphore类可以控制某个资源允许访问的线程数,Semaphore有命名式的,也有不命名的:如果不考虑跨进程工作,一般在代码中使用不命名方式即可. 信号量有点类似于等待句柄,某个线程如果调用了W ...
- 铁乐学python_Day42_线程-信号量事件条件
铁乐学python_Day42_线程-信号量事件条件 线程中的信号量 同进程的一样,Semaphore管理一个内置的计数器, 每当调用acquire()时内置计数器-1:调用release() 时内置 ...
- pytho线程信号量
pytho线程信号量 import threading,time def going(num,sleep_time): semaphore.acquire()#启动允许执行 print("g ...
- python线程信号量semaphore(33)
通过前面对 线程互斥锁lock / 线程事件event / 线程条件变量condition / 线程定时器timer 的讲解,相信你对线程threading模块已经有了一定的了解,同时执行多个线程的 ...
- python 线程信号量
线程信号量和进程信号量相似 # 线程信号量 import time from threading import Semaphore from threading import Thread def t ...
- 线程同步 –Mutex和Semaphore
上一篇介绍了同步事件EventWaitHandle,以及它的两个子类型AutoResetEvent和ManualResetEvent.下面接着介绍WaitHandle的另外两个子类型Mutex和Sem ...
- 线程互斥,mutex
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
随机推荐
- English sentence
For a better environment, we should teach our children to put litter/garbage/trash into dustbin/dust ...
- Linux Install VirtualBox
添加源: cd /etc/yum.repos.dwget http://download.virtualbox.org/virtualbox/rpm/rhel/virtualbox.repo 下面3个 ...
- 标清与高清,720p和1080p,720i和720p,h264与h265
480x320, 640x480 标清 1024x720p 高清 1920x1080i (隔行扫描) 也属于高清 1920x1080p 全高清 3840x2160,7680x4320 超(高)清 ...
- ArcGIS Viewer for Flex中引入google map作底图
在ArcGIS Viewer for Flex开发中,经常需要用到google map作为底图,我们不能通过ArcGIS Viewer for Flex - Application Builder轻易 ...
- java后台异步任务执行器TaskManager
java后台异步任务执行器TaskManager 此方式基于MVC方式: 一,使用任务: @Resource private TaskManager taskManager; public strin ...
- hive日期函数
今天select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss') UNIX时间戳转日期函数: from_unixtime 语法: from_ ...
- 2016-08-15:从YUV420P中提取指定大小区域
typedef struct { int width; int height; }SizeInfo; typedef struct { int x; int y; int width; int hei ...
- 关于php多线程的记录
最近需要对3W台服务器进行下发脚本,如果一个一个执行,时间大约在2个小时,特别的慢,于是修改程序,采用php的多线程去分发,大概在10分钟左右完成,下面记录下这次的经验和理解: 我所理解的php的多线 ...
- mongoDB 使用手册
1.基本操作db.AddUser(username,password) 添加用户db.auth(usrename,password) 设置数据库连接验证db.cloneDataBase(fromh ...
- IBindCtx接口定义
IBindCtx接口定义