易语言教程_易语言源码_易语言视频教程_易语言论坛

 找回密码
 点击注册

Vip新手入门区
新手学习指南  学员作品展示 Vip课程总纲  Vip绝密课程系列

Vip相关下载区
Vip模块下载   Vip模块绑定   Vip模块例子 魔鬼插件下载  魔鬼插件例子  教程工具下载

Vip论坛服务区
教程问题提问区   模块问题提问区 技术交流区   魔鬼插件建议   忘记密码找回

VIP会员办理QQ: 8643245   
【请先加好友,然后到好友列表双击联系客服,办理VIP会员。】
【基础篇】易语言辅助入门基础教程
VIP模块办理QQ: 7189694 办理正版魔鬼作坊VIP模块 【基础篇】OD与CE入门基础教程
办理【终身VIP会员】“秒杀价” 仅需 RMB278.00元… 【基础篇】零基础绝密汇编语言入门课程 (共26课已完成)…
办理VIP详情…猛击这里查看详情 【基础篇】VIP辅助入门基础教程-新手必学 已发布10课 ……
VIP教程免费试看章节…猛击下载 【第1款】制作“辅助挂”教程目录查看(共107+16_x64下更新课已完成)…
亲爱的VIP学员,请到此写下你学习的感受与发布作品截图… 【第2款】制作“任务挂”教程目录查看(共77+1_x64下更新课已完成)…
卍解吧!不用bp send类封包断点找CALL的各种通杀思路 【第3款】驱动过保护技术课程(共38课已完成)…
【绝密教程】VIP绝密教程系列---注意:随时会更新! 【第4款】VIP邪恶二叉树辅助课程 (共31+17_x64下更新课已完成)…
【精品第13款】3D射击游戏与页游透视 智辅课程 已完成17课… 【第5款】零基础易语言按键辅助教程 (30课已完成)…
【精品第14款】变态功能辅助是如何炼成的 已完成36课… 【第6款】从零开始学习封包辅助技术教程(20课已完成) …
【精品第15款】DNF商业变态辅助的修炼之路 已完成27课… 【第7款】大杀特杀分析来源与CALL吸血鬼课程 (56课已完成)
【精品第16款】中控台多线程多开自动化商业辅助课程 已完成66课… 【第8款】完全零基础网页辅助课程(40课已完成)
【全新精品第17款】检测原理与过游戏内存检测技术课程 已发布9课… 【第9款】自动登录与操控LUA技术课程 (共46+8_x64下更新课已完成)…
【全新精品第18款】手游全自动化任务脚本辅助课程 已发布25课…… 【第10款】网页辅助封包脱机进阶课程 已完成30课…
【全新精品第19款】D3D方框骨骼透视与自瞄辅助课程进阶篇 已发布34课…… 【第11款】VC++ Lua脚本辅助课程 已完成112课…
【全新精品第20款】 X64模拟器吃鸡游戏方框透视自瞄辅助课程 发布中... 【第12款】网游脱机封包智辅课程 已完成35课…
查看: 1366|回复: 0

CreateMutex WaitForSingleObject ReleaseMutex使用

[复制链接]

20

主题

4

回帖

32

积分

编程入门

Rank: 1

魔鬼币
574
发表于 2016-7-2 17:04:44 | 显示全部楼层 |阅读模式
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在没有任何线程拥有他的时候,他是处于激发态的, 所以处于有信号状态。
您需要登录后才可以回帖 登录 | 点击注册

本版积分规则

魔鬼作坊|易语言教程|易语言源码|易语言论坛|易语言视频教程| 论坛导航|免责申明|手机版||网站地图
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表魔鬼作坊立场!
任何人不得以任何方式翻录、盗版或出售本站视频,一经发现我们将追究其相关责任!
我们一直在努力成为最好的编程论坛!
Copyright© 2010-2019 All Right Reserved.
快速回复 返回顶部 返回列表