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; ...
随机推荐
- c# Wndproc的使用方法
protected override void WndProc(ref Message m) { const int WM_SYSCOMMAND = 0x0112; const int SC_CLOS ...
- ORACLE随机查询
1. select * from (select * from tablename order by dbms_random.value) where rownum< N; 注:dbms_ran ...
- UVA 753 UNIX 插头(EK网络流+Floyd传递闭包)
UNIX 插头 紫书P374 [题目链接]UNIX 插头 [题目类型]EK网络流+Floyd传递闭包 &题解: 看了书之后有那么一点懂了,但当看了刘汝佳代码后就完全明白了,感觉他代码写的好牛逼 ...
- 通过dblink的方式查看表的结构
有dba权限: SELECT * FROM DBA_TAB_COLUMNS@DBLINK_TEST WHERE TABLE_NAME = '表名'; 没有dba权限:SELECT * FROM USE ...
- 实用的VS工具
工具 1.Visual Studio Visual Studio Productivity Power tool:Visual Studio专业版(及以上)的扩展,具有丰富的功能,如快速查找,导航解决 ...
- linux命令行与shell脚本编程大全---更多bash shell命令
进程状态:0代表正在运行:S代表在休眠:R代表可运行,正等待运行:Z代表僵化,进程已经结束但父进程已不存在:T代表停止. 查看有那些进程运行:ps -ef 基本的linux文件系统: 1.ext文件 ...
- silverlight简单数据绑定2
<Grid x:Name="LayoutRoot" Background="white" Loaded="LayoutRoot_Loaded&q ...
- Jmeter安装
安装下载方法:http://www.jb51.net/softjc/266834.html 下载地址:http://jmeter.apache.org/download_jmeter.cgi Wind ...
- strong & weak
Here is a quick summary: A strong reference will keep the object it points to from being deallocated ...
- git 相关
github入门 一.先了解 相比CVS\SVN优势: - 支持离线开发,离线Repository- 强大的分支功能,适合多个独立开发者协作- 速度快 github 本地有仓库,储存着所有repo ...