转载:CreateMutex WaitForSingleObject ReleaseMutex使用
HANDLE CreateMutex(
LPSECURITY_ATTRIBUTES lpMutexAttributes,//
BOOL bInitialOwner, // flag for initial ownership
LPCTSTR lpName // pointer to mutex-object name
);
参数2:指示互斥对象的初始拥有者。 如果该值是真,调用者创建互斥对象,调用的线程获得互斥对象的所有权。 否则,不拥有所有权,此时互斥对象处于空闲状态,其他线程可以占用。
(-) 主线程中创建拥有所有权的互斥量,两个子线程中分别等待互斥量-》没有输出
DWORD WINAPI ThreadProc1(LPVOID lpParameter);
DWORD WINAPI ThreadProc2(LPVOID lpParameter); int ticket = ;
HANDLE hMutex = NULL; int _tmain(int argc, _TCHAR* argv[])
{
HANDLE handle1 = CreateThread(NULL,,ThreadProc1,NULL,,NULL);
HANDLE handle2 = CreateThread(NULL,,ThreadProc2,NULL,,NULL); CloseHandle(handle1);
CloseHandle(handle2); hMutex = CreateMutex(NULL,TRUE,NULL); //第二个参数为TRUE,互斥对象的所有权为主线程所有,非空闲状态 Sleep(); return ;
} DWORD WINAPI ThreadProc1(LPVOID lpParameter)
{ //WaitForSingleObject(hMutex,INFINITE); //第二个参数为INFINITE表示一直等待,直到拥有互斥对象
while(TRUE)
{ WaitForSingleObject(hMutex,INFINITE); //第二个参数为INFINITE表示一直等待,直到拥有互斥对象 if ( ticket > )
{
Sleep(); printf("thread1 sale the ticket id is: %d\n", ticket--);
}
else
{
break;
} ReleaseMutex(hMutex); //使用完了,将互斥对象还给操作系统
}
//ReleaseMutex(hMutex); //使用完了,将互斥对象还给操作系统
return ;
} DWORD WINAPI ThreadProc2(LPVOID lpParameter)
{ while(TRUE)
{ WaitForSingleObject(hMutex,INFINITE); //第二个参数为INFINITE表示一直等待,直到拥有互斥对象 if ( ticket > )
{ Sleep(); printf("thread2 sale the ticket id is: %d\n", ticket--); }
else
{
break;
} ReleaseMutex(hMutex); //使用完了,将互斥对象还给操作系统
} return ;
}
(二) 主线程中创建没有所有权的互斥量,两个子线程中分别等待互斥量-》输出如下
thread1 sale the ticket id is: 50
thread2 sale the ticket id is: 49
thread1 sale the ticket id is: 48
thread2 sale the ticket id is: 47
thread1 sale the ticket id is: 46
thread2 sale the ticket id is: 45
thread1 sale the ticket id is: 44
thread2 sale the ticket id is: 43
thread1 sale the ticket id is: 42
thread2 sale the ticket id is: 41
thread1 sale the ticket id is: 40
thread2 sale the ticket id is: 39
thread1 sale the ticket id is: 38
thread2 sale the ticket id is: 37
thread1 sale the ticket id is: 36
thread2 sale the ticket id is: 35
thread1 sale the ticket id is: 34
thread2 sale the ticket id is: 33
thread1 sale the ticket id is: 32
thread2 sale the ticket id is: 31
thread1 sale the ticket id is: 30
thread2 sale the ticket id is: 29
thread1 sale the ticket id is: 28
thread2 sale the ticket id is: 27
thread1 sale the ticket id is: 26
thread2 sale the ticket id is: 25
thread1 sale the ticket id is: 24
thread2 sale the ticket id is: 23
thread1 sale the ticket id is: 22
thread2 sale the ticket id is: 21
thread1 sale the ticket id is: 20
thread2 sale the ticket id is: 19
thread1 sale the ticket id is: 18
thread2 sale the ticket id is: 17
thread1 sale the ticket id is: 16
thread2 sale the ticket id is: 15
thread1 sale the ticket id is: 14
thread2 sale the ticket id is: 13
thread1 sale the ticket id is: 12
thread2 sale the ticket id is: 11
thread1 sale the ticket id is: 10
thread2 sale the ticket id is: 9
thread1 sale the ticket id is: 8
thread2 sale the ticket id is: 7
thread1 sale the ticket id is: 6
thread2 sale the ticket id is: 5
thread1 sale the ticket id is: 4
thread2 sale the ticket id is: 3
thread1 sale the ticket id is: 2
thread2 sale the ticket id is: 1
(三) 主线程中创建没有所有权的互斥量,主线程和子线程中分别等待互斥量-》主线程和子线程交替输出
(四) 主线程中创建拥有所有权的互斥量,主线程和子线程中分别等待互斥量-》只有主线程输出
这个原因不知道如何解释,难道在主线程中创建有所有权的,其他线程就永远等待不到了吗
(五) 子线程中创建拥有所有权的互斥量,主线程和子线程中分别等待互斥量-》只有子线程输出
(四)和(五)的结果可以说明在哪个线程中创建拥有所有权的互斥量,所有权永远被此线程占有,即使释放了互斥量。
以上结果都在Wince6.0测试。
后来找到一个 说明,貌似可以说明以上结论呢:
如创建进程希望立即拥有互斥体,则设为TRUE。一个互斥体同时只能由一个线程拥有。是FALSE,表示刚刚创建的这个Mutex不属于任何线程 也就是没有任何线程拥有他,一个Mutex在没有任何线程拥有他的时候,他是处于激发态的, 所以处于有信号状态。
转载:CreateMutex WaitForSingleObject ReleaseMutex使用的更多相关文章
- CreateMutex() 、ReleaseMutex()
功能: CreateMutex() 用于有独占要求的程序 (在其进程运行期间不允许其他使用此端口设备的程序运行,或不允许同名程序运行). 比如运行金山词霸时,一次只能运行一个实例,当运行第二个实例时, ...
- 零基础逆向工程38_Win32_12_信号量_线程控制小结
1 信号量 信号量(Semaphore),有时被称为信号灯,是在多线程环境下使用的一种设施,是可以用来保证两个或多个关键代码段不被并发调用.[百度百科] 1.1 创建信号量 HANDLE Create ...
- 事件EVENT与waitforsingleobject的使用以及Mutex与Event的区别
Mutex与Event控制互斥事件的使用详解 最近写一程序,误用了Mutex的功能,错把Mutex当Event用了. [Mutex] 使用Mutex的主要函数:CreateMutex.ReleaseM ...
- windows多线程同步--互斥量
关于互斥量的基本概念:百度百科互斥量 推荐参考博客:秒杀多线程第七篇 经典线程同步 互斥量Mutex 注意:互斥量也是一个内核对象,它用来确保一个线程独占一个资源的访问.互斥量与关键段的行为非常相似, ...
- 如何创建一个简单的C++同步锁框架(译)
翻译自codeproject上面的一篇文章,题目是:如何创建一个简单的c++同步锁框架 目录 介绍 背景 临界区 & 互斥 & 信号 临界区 互斥 信号 更多信息 建立锁框架的目的 B ...
- 多线程编程之Windows同步方式
在Windows环境下针对多线程同步与互斥操作的支持,主要包括四种方式:临界区(CriticalSection).互斥对象(Mutex).信号量(Semaphore).事件对象(Event).下面分别 ...
- c++一些面试题目
1.What is achieved by prefixing the 'static' keyword to a file-level function or file-level variable ...
- Windows核心编程:第9章 用内核对象进行线程同步
Github https://github.com/gongluck/Windows-Core-Program.git //第9章 用内核对象进行线程同步.cpp: 定义应用程序的入口点. // #i ...
- 多线程的那点儿事(之windows锁)
在windows系统中,系统本身为我们提供了很多锁.通过这些锁的使用,一方面可以加强我们对锁的认识,另外一方面可以提高代码的性能和健壮性.常用的锁以下四种:临界区,互斥量,信号量,event. (1) ...
随机推荐
- Azure资源管理工具Azure PowerShell介绍
什么是 Azure PowerShell? Azure PowerShell 是一组模块,提供用于通过 Windows PowerShell 管理 Azure 的 cmdlet.你可以使用 cmdle ...
- SQL Server中变量的声明和使用方法
声明局部变量语法:DECLARE @variable_name DataType其中 variable_name为局部变量的名称,DataType为数据类型.给局部变量赋值有两种方法:1.SET @v ...
- Servlet和JSP之JSTL学习
JSTL JSTL就是JSP标准标签库(JavaServer Pages Standard Tag Library, JSTL)是一个定制标签库的集合,用来解决像遍历Map或集合.条件测试.XML处理 ...
- iOS打包上传app store各种问题解决总结
问题1 this action could not be completed. try again 问题2 there was an error sending data to the iTunes ...
- (转)SpringMVC学习(四)——Spring、MyBatis和SpringMVC的整合
http://blog.csdn.net/yerenyuan_pku/article/details/72231763 之前我整合了Spring和MyBatis这两个框架,不会的可以看我的文章MyBa ...
- Gym 100342E Minima (暴力,单调队列)
3e7暴力,800ms+过,单调队列维护区间最小值. #include<bits/stdc++.h> using namespace std; typedef long long ll; ...
- hdu 6058 Kanade's sum (计算贡献,思维)
题意: 给你一个全排列,要你求这个序列的所有区间的第k大的和 思路:比赛的时候一看就知道肯定是算贡献,也知道是枚举每个数,然后看他在多少个区间是第K大,然后计算他的贡献就可以了,但是没有找到如何在o( ...
- CS 分解
将学习到什么 CS 分解是分划的酉矩阵在分划的酉等价之下的标准型. 它的证明涉及奇异值分解.QR 分解以及一个简单习题. 一个直观的习题 设 \(\Gamma, L \in M_p\). 假设 \ ...
- PAT (Basic Level) Practise (中文)- 1026. 程序运行时间(15)
PAT (Basic Level) Practise (中文)- 1026. 程序运行时间(15) http://www.patest.cn/contests/pat-b-practise/10 ...
- 洛谷五月月赛【LGR-047】划水记
虽然月赛有些爆炸,但我永远资瓷洛谷! 因为去接水,所以迟到了十几分钟,然后洛谷首页就打不开了-- 通过洛谷题库间接打开了比赛,看了看\(TA\),WTF?博弈论?再仔细读了读题,嗯,判断奇偶性,不过要 ...