龙马谷

 找回密码
 立即注册

QQ登录

只需一步,快速开始

龙马谷VIP会员办理客服QQ:82926983
1 [已完结] GG修改器新手入门与实战教程 31课 2 [已完结] GG修改器美化修改教程 6课 3 [已完结] GG修改器Lua脚本新手入门教程 12课
4 [已完结] 触动精灵脚本新手入门必学教程 22课 5 [已完结] 手游自动化脚本入门实战教程 9课 6 [已完结] C++射击游戏方框骨骼透视与自瞄教程 27课
7 [已完结] C++零基础UE4逆向开发FPS透视自瞄教程 29课 8 [已完结] C++零基础大漠模拟器手游自动化辅助教程 22课
以下是天马阁VIP教程,本站与天马阁合作,赞助VIP可以获得天马阁对应VIP会员,名额有限! 点击进入天马阁论坛
1 [已完结] x64CE与x64dbg入门基础教程 7课 2 [已完结] x64汇编语言基础教程 16课 3 [已完结] x64辅助入门基础教程 9课
4 [已完结] C++x64内存辅助实战技术教程 149课 5 [已完结] C++x64内存检测与过检测技术教程 10课 6 [已完结] C+x64二叉树分析遍历与LUA自动登陆教程 19课
7 [已完结] C++BT功能原理与x64实战教程 29课 8 [已完结] C+FPS框透视与自瞄x64实现原理及防护思路
查看: 1751|回复: 0

CreateMutex WaitForSingleObject ReleaseMutex使用

[复制链接]

26

主题

1

回帖

33

积分

编程入门

Rank: 1

龙马币
62

CreateMutex WaitForSingleObject ReleaseMutex使用

  1. HANDLE CreateMutex(
  2. LPSECURITY_ATTRIBUTES lpMutexAttributes,
  3. BOOL bInitialOwner,  // flag for initial ownership
  4. LPCTSTR lpName     // pointer to mutex-object name
  5. );
复制代码


参数2:指示互斥对象的初始拥有者。 如果该值是真,调用者创建互斥对象,调用的线程获得互斥对象的所有权。 否则,不拥有所有权,此时互斥对象处于空闲状态,其他线程可以占用。

(-)  主线程中创建拥有所有权的互斥量,两个子线程中分别等待互斥量-》没有输出

  1. DWORD WINAPI ThreadProc1(LPVOID lpParameter);  
  2. DWORD WINAPI ThreadProc2(LPVOID lpParameter);  
  3.   
  4. int    ticket = 50;  
  5. HANDLE hMutex = NULL;  
  6.   
  7. int _tmain(int argc, _TCHAR* argv[])  
  8. {  
  9.     HANDLE handle1 = CreateThread(NULL,0,ThreadProc1,NULL,0,NULL);  
  10.     HANDLE handle2 = CreateThread(NULL,0,ThreadProc2,NULL,0,NULL);  
  11.     CloseHandle(handle1);  
  12.     CloseHandle(handle2);  
  13.     hMutex = CreateMutex(NULL,TRUE,NULL); //第二个参数为TRUE,互斥对象的所有权为主线程所有,非空闲状态  
  14.     Sleep(4000);  
  15.     return 0;  
  16. }  
  17.   
  18. DWORD WINAPI ThreadProc1(LPVOID lpParameter)  
  19. {   
  20.     while(TRUE)  
  21.     {  
  22.         WaitForSingleObject(hMutex,INFINITE); //第二个参数为INFINITE表示一直等待,直到拥有互斥对象  
  23.         if ( ticket > 0 )  
  24.         {  
  25.             Sleep(1);  
  26.             printf("thread1 sale the ticket id is: %d\n", ticket--);  
  27.         }  
  28.         else  
  29.         {  
  30.             break;  
  31.         }                  
  32.         ReleaseMutex(hMutex); //使用完了,将互斥对象还给操作系统  
  33.     }  
  34.     return 0;  
  35. }   


  36. DWORD WINAPI ThreadProc2(LPVOID lpParameter)  
  37. {  
  38.     while(TRUE)  
  39.     {  
  40.         WaitForSingleObject(hMutex,INFINITE); //第二个参数为INFINITE表示一直等待,直到拥有互斥对象           
  41.         if ( ticket > 0 )  
  42.         {  
  43.            Sleep(1);  
  44.            printf("thread2 sale the ticket id is: %d\n", ticket--);  
  45.         }  
  46.         else  
  47.         {  
  48.             break;  
  49.         }         
  50.         ReleaseMutex(hMutex); //使用完了,将互斥对象还给操作系统  
  51.     }  
  52.     return 0;  
  53. }  
复制代码



(二)  主线程中创建没有所有权的互斥量,两个子线程中分别等待互斥量-》输出如下

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

(三)  主线程中创建没有所有权的互斥量,主线程和子线程中分别等待互斥量-》主线程和子线程交替输出

(四)  主线程中创建拥有所有权的互斥量,主线程和子线程中分别等待互斥量-》只有主线程输出
这个原因不知道如何解释,难道在主线程中创建有所有权的,其他线程就永远等待不到了吗

(五) 子线程中创建拥有所有权的互斥量,主线程和子线程中分别等待互斥量-》只有子线程输出

(四)和(五)的结果可以说明在哪个线程中创建拥有所有权的互斥量,所有权永远被此线程占有,即使释放了互斥量。

后来找到一个 说明,貌似可以说明以上结论呢:

如创建进程希望立即拥有互斥体,则设为TRUE。
一个互斥体同时只能由一个线程拥有。
是FALSE,表示刚刚创建的这个Mutex不属于任何线程 也就是没有任何线程拥有他,一个Mutex在没有任何线程拥有他的时候,他是处于激发态的, 所以处于有信号状态。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

龙马谷| C/C++辅助教程| 安卓逆向安全| 论坛导航| 免责申明|Archiver|
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表龙马谷立场!
任何人不得以任何方式翻录、盗版或出售本站视频,一经发现我们将追究其相关责任!
我们一直在努力成为最好的编程论坛!
Copyright© 2018-2021 All Right Reserved.
在线客服
快速回复 返回顶部 返回列表