转载: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) ...
随机推荐
- 洛谷 P1309 瑞士轮
题目背景 在双人对决的竞技性比赛,如乒乓球.羽毛球.国际象棋中,最常见的赛制是淘汰赛和循环赛.前者的特点是比赛场数少,每场都紧张刺激,但偶然性较高.后者的特点是较为公平,偶然性较低,但比赛过程往往十分 ...
- java反序列化字节转字符串工具
https://github.com/NickstaDB/SerializationDumper SerializationDumper-v1.1.jar 用法 : java -jar Seriali ...
- CPP-网络/通信:COM
))//打开串口 { ) { CloseCom();//关闭串口 break; } //添加处理代码. } //最后关闭串口 CloseCom();//关闭串口
- a标签点击后更改颜色
function choose(id){ document.getElementById("typeid").value = id; //var infoa=document.ge ...
- 字符串数组 输入3个字符串,要求按由小到大的字母顺序输出; 输入n个学生的姓名和学号到字符串数组中,在输入一个姓名,如果班级有该生则返回其信息,否则返回本班无此人
输入3个字符串,要求按由小到大的字母顺序输出 如 输入franch england china,输出结果是china england franch 三个数排序输出,比较三个数的大小怎么做? a=18 ...
- Qt读写excel
今天在利用Qt进行excel操作时,代码总是走到打开excel这一步是总是出现程序崩溃.在网上查找了各种帖子 说法不一,尝试都没有解决.后来猜想是不是excel没有激活影响的.发现自己的excel没 ...
- 【贪心 思维题】[USACO13MAR]扑克牌型Poker Hands
看似区间数据结构的一道题 题目描述 Bessie and her friends are playing a unique version of poker involving a deck with ...
- 【Java_基础】java中的多态性
方法的重载.重写和动态链接构成了java的多态性. 1.方法的重载 同一个类中多个同名但形参有所差异的方法,在调用时会根据参数的不同做出选择. 2.方法的重写 子类中重新定义了父类的方法,有关方法重写 ...
- RN服务
https://facebook.github.io/react-native/docs/headless-js-android.html 当app在 后台运行 时,我们可以使用RN服务来同时地刷新数 ...
- [转]Makefile中的wildcard/notdir/patsubst
1.wildcard : 扩展通配符 2.notdir : 去除路径 3.patsubst :替换通配符 例子:建立一个测试目录,在测试目录下建立一个名为sub的子目录$ mkdir test$ cd ...