天马阁

 找回密码
 立即注册
                                        →→→→→→→→→→→→ 1点击查看所有VIP教程目录长列表(总教程数269个) 2办理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实现原理及防护思路 30课完结
64驱?封? 9 64反驱? 10 64位V? 11 绝? 12 ???课?
13 64透 ? 14 64U ? 15 64Q ? 16 64功 ?
17 64U ? 18 64模 ? 19 64多 ? 20 64网 ?
21 64注 ? 22 64火 ? 23 64棋 ? 24 64自二链L?
25 64破 ? VIP会员办理QQ: 89986068   
【请先加好友,然后到好友列表双击联系客服办理,不然可能无法接受到信息。】
27 加入2000人交流群637034024 3 28 免责声明?
查看: 1160|回复: 0

CreateMutex WaitForSingleObject ReleaseMutex使用

[复制链接]

14

主题

0

回帖

17

积分

编程入门

Rank: 1

天马币
28
发表于 2024-3-1 14:10:29 | 显示全部楼层 |阅读模式
HANDLE CreateMutex(

LPSECURITY_ATTRIBUTES lpMutexAttributes,//

BOOL bInitialOwner,  // flag for initial ownership

LPCTSTR lpName     // pointer to mutex-object name

);

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

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

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
DWORD WINAPI ThreadProc1(LPVOID lpParameter);  
DWORD WINAPI ThreadProc2(LPVOID lpParameter);  
  
int    ticket = 50;  
HANDLE hMutex = NULL;  
  
int _tmain(int argc, _TCHAR* argv[])  
{  
    HANDLE handle1 = CreateThread(NULL,0,ThreadProc1,NULL,0,NULL);  
    HANDLE handle2 = CreateThread(NULL,0,ThreadProc2,NULL,0,NULL);  
  
    CloseHandle(handle1);  
    CloseHandle(handle2);  
  
    hMutex = CreateMutex(NULL,TRUE,NULL); //第二个参数为TRUE,互斥对象的所有权为主线程所有,非空闲状态  
  
    Sleep(4000);  
  
    return 0;  
}  
  
DWORD WINAPI ThreadProc1(LPVOID lpParameter)  
{   
    while(TRUE)  
    {  
  
        WaitForSingleObject(hMutex,INFINITE); //第二个参数为INFINITE表示一直等待,直到拥有互斥对象  
  
        if ( ticket > 0 )  
        {  
            Sleep(1);  
  
            printf("thread1 sale the ticket id is: %d\n", ticket--);  
        }  
        else  
        {  
            break;  
        }                  
  
        ReleaseMutex(hMutex); //使用完了,将互斥对象还给操作系统  
    }  
    return 0;  
}   
  
DWORD WINAPI ThreadProc2(LPVOID lpParameter)  
{  
  
    while(TRUE)  
    {  
        WaitForSingleObject(hMutex,INFINITE); //第二个参数为INFINITE表示一直等待,直到拥有互斥对象           
  
        if ( ticket > 0 )  
        {  
  
           Sleep(1);  
  
           printf("thread2 sale the ticket id is: %d\n", ticket--);  
  
        }  
        else  
        {  
            break;  
        }         
  
        ReleaseMutex(hMutex); //使用完了,将互斥对象还给操作系统  
    }  
  
    return 0;  
}  

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

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© 2010-2021 All Right Reserved.
快速回复 返回顶部 返回列表