mutex 互斥量
有用参考:http://blog.csdn.net/yl2isoft/article/details/46003467
摘抄记录:
using System.Threading;
class Example
{
    // Create a new Mutex. The creating thread does not own the mutex. 
    private static Mutex mut = new Mutex();
    private const int numIterations = 1;
    private const int numThreads = 3;
static void Main()
    {
        // Create the threads that will use the protected resource. 
        for(int i = 0; i < numThreads; i++)
        {
            Thread newThread = new Thread(new ThreadStart(ThreadProc));
            newThread.Name = String.Format("Thread{0}", i + 1);
            newThread.Start();
        }
// The main thread exits, but the application continues to 
        // run until all foreground threads have exited.
    }
private static void ThreadProc()
    {
        for(int i = 0; i < numIterations; i++)
        {
            UseResource();
        }
    }
// This method represents a resource that must be synchronized 
    // so that only one thread at a time can enter. 
    private static void UseResource()
    {
        // Wait until it is safe to enter, and do not enter if the request times out.
        Console.WriteLine("{0} is requesting the mutex", Thread.CurrentThread.Name);
        if (mut.WaitOne(1000)) {
           Console.WriteLine("{0} has entered the protected area", 
               Thread.CurrentThread.Name);
// Place code to access non-reentrant resources here.
// Simulate some work.
           Thread.Sleep(5000);
Console.WriteLine("{0} is leaving the protected area", 
               Thread.CurrentThread.Name);
// Release the Mutex.
              mut.ReleaseMutex();
           Console.WriteLine("{0} has released the mutex", 
                             Thread.CurrentThread.Name);
        }
        else {
           Console.WriteLine("{0} will not acquire the mutex", 
                             Thread.CurrentThread.Name);
        }
    }
}
mutex 互斥量的更多相关文章
- Linux并发与同步专题 (4) Mutex互斥量
		
关键词:mutex.MCS.OSQ. <Linux并发与同步专题 (1)原子操作和内存屏障> <Linux并发与同步专题 (2)spinlock> <Linux并发与同步 ...
 - 【Linux】Mutex互斥量线程同步的例子
		
0.互斥量 Windows下的互斥量 是个内核对象,每次WaitForSingleObject和ReleaseMutex时都会检查当前线程ID和占有互斥量的线程ID是否一致. 当多次Wait**时就 ...
 - C++多线程同步之Mutex(互斥量)
		
原文链接: http://blog.csdn.net/olansefengye1/article/details/53086141 一.互斥量Mutex同步多线程 1.Win32平台 相关函数和头文件 ...
 - php Pthread 多线程 (三) Mutex 互斥量
		
当我们用多线程操作同一个资源时,在同一时间内只能有一个线程能够对资源进行操作,这时就需要用到互斥量了.比如我们对同一个文件进行读写操作时. <?php class Add extends Thr ...
 - c# Thread5——线程同步之基本原子操作。Mutex互斥量的使用
		
之前的博文也说到了如果多线程对于访问的公共资源操作都是原子操作,那么可以避免竞争条件.关于多线程的竞争可以百度. 1.执行最基本的原子操作 c#提供了一系列供我们使用的原子操作的方法和类型,比如我们的 ...
 - 多线程相关------互斥量Mutex
		
互斥量(Mutex) 互斥量是一个可以处于两态之一的变量:解锁和加锁.只有拥有互斥对象的线程才具有访问资源的权限.并且互斥量可以用于不同进程中的线程的互斥访问. 相关函数: CreateMutex用于 ...
 - posix thread互斥量
		
互斥量 互斥量(Mutex)是“mutual exclusion”的缩写.互斥量是实现线程同步,和保护同时写共享数据的主要方法.使用互斥量的典型顺序如下:1. 创建和初始一个互斥量 2. 多个线程尝试 ...
 - linux下多线程互斥量实现生产者--消费者问题和哲学家就餐问题
		
生产者消费者问题,又有界缓冲区问题.两个进程共享一个一个公共的固定大小的缓冲区.其中一个是生产者,将信息放入缓冲区,另一个是消费者,从缓冲区中取信息. 问题的关键在于缓冲区已满,而此时生产者还想往其中 ...
 - Linux多线程实践(5) --Posix信号量与互斥量解决生产者消费者问题
		
Posix信号量 Posix 信号量 有名信号量 无名信号量 sem_open sem_init sem_close sem_destroy sem_unlink sem_wait sem_post ...
 
随机推荐
- Mysql 隐式转换
			
表定义: CREATE TABLE `ids` ( id ) not null auto_increment, PRIMARY KEY (id) ); 表中存在一些IDs: 111, 112, 113 ...
 - Windows环境安装Django步骤
			
前提:已经安装Python 1.先从Django官网下载压缩包:https://www.djangoproject.com/download/ 2.解压Django,如我解压到 D:\Python\D ...
 - Intellij IDEA编辑golang时无法加载系统GOPATH变量
			
问题: 编译go项目时,报找不到包.从日志看,GOPATH与系统设置的不一致. 如何解决:系统的gopath路径,加到Project libraries中 参考:https://segmentfaul ...
 - 机器学习进阶-图像形态学操作-腐蚀操作 1.cv2.erode(进行腐蚀操作)
			
1.cv2.erode(src, kernel, iteration) 参数说明:src表示的是输入图片,kernel表示的是方框的大小,iteration表示迭代的次数 腐蚀操作原理:存在一个ker ...
 - EMQ笔记
			
飞行窗口(Inflight Window)保存当前正在发送未确认的Qos1/2消息.窗口值越大,吞吐越高:窗口值越小,消息顺序越严格. 当客户端离线或者飞行窗口(Inflight Window)满时, ...
 - epoll_wait 时 POLLERR 与 POLLIN 同时返回的现象解析(转)
			
今天code review时,同事B对我代码中的poll()的处理做法提出了异议.于是做了些研究,还发现了一些好玩的故事. 异议的代码 我的代码是参考manpage写的,类似下面的做法.同事B说没有处 ...
 - matt cutts : try something new for 30 days
			
30 天尝试新事物matt cutts : try something new for 30 days[小计划帮你实现大目标] 是否有些事情, 你一直想去做, 但就是没有实现?马特 ?卡茨建议: 尝试 ...
 - Python 3 学习笔记(3)
			
模块 编写模块 # fibo.py # Fibonacci numbers module def fib(n): # write Fibonacci series up to n a, b = 0, ...
 - Kotlin语言学习笔记(2)
			
类(classes) // 类声明 class Invoice { } // 空的类 class Empty // 主体构造器(primary constructor) class Person co ...
 - android填满手机内存的方法
			
1. 进行临界测试,手机盘空间存满的条件下应用会有何表现:通常手动添加大文件但是还是不够,通过如下 2. 使用adb命令完成:通过如下 adb 命令在 /mnt/sdcard/ 目录下产生一个名为 b ...