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的更多相关文章

  1. 四十三、Linux 线程——线程同步之线程信号量

    43.1 信号量 43.1.1 信号量介绍 信号量从本质上是一个非负整数计数器,是共享资源的数目,通常被用来控制对共享资源的访问 信号量可以实现线程的同步和互斥 通过 sem_post() 和 sem ...

  2. UDP、线程、mutex锁(day15)

    一.基于UDP的网络编程模型 服务器端 .创建socket. .将fd和服务器的ip地址和端口号绑定 .recvfrom阻塞等待接收客户端数据 .业务处理 .响应客户端 客户端: .创建socket ...

  3. 【.NET深呼吸】线程信号量(Semaphore)

    Semaphore类可以控制某个资源允许访问的线程数,Semaphore有命名式的,也有不命名的:如果不考虑跨进程工作,一般在代码中使用不命名方式即可. 信号量有点类似于等待句柄,某个线程如果调用了W ...

  4. 铁乐学python_Day42_线程-信号量事件条件

    铁乐学python_Day42_线程-信号量事件条件 线程中的信号量 同进程的一样,Semaphore管理一个内置的计数器, 每当调用acquire()时内置计数器-1:调用release() 时内置 ...

  5. pytho线程信号量

    pytho线程信号量 import threading,time def going(num,sleep_time): semaphore.acquire()#启动允许执行 print("g ...

  6. python线程信号量semaphore(33)

    通过前面对 线程互斥锁lock /  线程事件event / 线程条件变量condition / 线程定时器timer 的讲解,相信你对线程threading模块已经有了一定的了解,同时执行多个线程的 ...

  7. python 线程信号量

    线程信号量和进程信号量相似 # 线程信号量 import time from threading import Semaphore from threading import Thread def t ...

  8. 线程同步 –Mutex和Semaphore

    上一篇介绍了同步事件EventWaitHandle,以及它的两个子类型AutoResetEvent和ManualResetEvent.下面接着介绍WaitHandle的另外两个子类型Mutex和Sem ...

  9. 线程互斥,mutex

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

随机推荐

  1. JS运动从入门到精髓!哈哈

    首先来看最基础的运动:单个物体向右匀速运动到某一点停止      例子:一个按钮,一个div,点击按钮,让div向右运动到某一个位置暂停 // 原理: 1 获取物体当前的位置 oDiv.offsetl ...

  2. java基础要点

    Java语言是现在比较常用的编程语言,因为Java语言可以编写桌用程序,也可以编写web程序,还能编写嵌入式程序.这是由于相比脚本语言,Java 的运行速度要快.而对于底层语言,Java与平台无关,可 ...

  3. git 强制回退服务器上的commit

    假设你有3个commit如下: commit 3 commit 2 commit 1   其中最后一次提交commit 3是错误的,那么可以执行: git reset --hard HEAD~1 你会 ...

  4. 【解决】SharePoint集成模式下Reporting Service—为用户授予的权限不足,无法执行此操作。 (rsAccessDenied)

    环境:Windows Server 2008 R2 SP1,SharePoint 2010 企业版,SQL Server 2008 R2 Reporting Service(SharePoint集成模 ...

  5. maven部署tomcat项目,403错误解决

    maven部署tomcat项目时403错误的解决方法 web模块的pom文件 pom.xml <plugin> <groupId>org.apache.tomcat.maven ...

  6. Good Bye 2013 A

    A. New Year Candles time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. ThinkPHP的路由形式

    首先解释一下,这里路由的意思是:系统从URL参数中分析出当前请求的分组.控制器.操作 .另外我的网址根目录是放在article目录下的,Thinkphp主要有下面几种路由形式 1.pathinfo路径 ...

  8. 简单实用的Windows命令(二)

    昨天简单的记录了几个非常简单实用的Windows命令,不过我又想起来还有两个我在实际的工作中也是经常用到的命令——PING和IPCONFIG,不过我在工作中的使用都是非常简单的,用PING命令检测对应 ...

  9. 一个CString的实现 拷贝构造函数的应用

    class CString { public: CString (char* s); CString(); ~CString(); private: char *str; int len; stati ...

  10. JS,Jquery获取各种屏幕的宽度和高度

    Javascript: 网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.b ...