CreateMutex WaitForSingleObject ReleaseMutex使用「建议收藏」

CreateMutex WaitForSingleObject ReleaseMutex使用「建议收藏」HANDLECreateMutex( LPSECURITY_ATTRIBUTESlpMutexAttributes,// BOOLbInitialOwner, //flagforinitialownership LPCTSTRlpName    //pointertomutex-objectname );

大家好,又见面了,我是你们的朋友全栈君。

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 = 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)
{

	//WaitForSingleObject(hMutex,INFINITE); //第二个参数为INFINITE表示一直等待,直到拥有互斥对象
	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); //使用完了,将互斥对象还给操作系统
	}
	//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

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

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

这个原因不知道如何解释,难道在主线程中创建有所有权的,其他线程就永远等待不到了吗

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

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

以上结果都在Wince6.0测试。

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/153184.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Keras中创建LSTM模型的步骤[通俗易懂]

    Keras中创建LSTM模型的步骤[通俗易懂]目录写在前面概述环境1、定义网络2、编译网络3、训练网络4、评估网络5、进行预测一个LSTM示例总结写在前面本文是对The5StepLife-CycleforLongShort-TermMemoryModelsinKeras的翻译,新手博主,边学边记,以便后续温习,或者对他人有所帮助概述深度学习神经网络在Python中很容易使用Keras创建和评估,但您必须遵循严格的模型生命周期。在这篇文章中,您将了解创建、训练和评估Keras中长期记忆(LSTM)循环神经网络的分步生

    2025年9月8日
    5
  • nginx https配置后无法访问,可能防火墙在捣鬼

    nginx https配置后无法访问,可能防火墙在捣鬼

    2021年10月13日
    45
  • CentOS 6安装gcc 4.7[通俗易懂]

    CentOS 6安装gcc 4.7

    2022年2月22日
    45
  • mybatis一级缓存和二级缓存失效_mybatis二级缓存默认开启吗

    mybatis一级缓存和二级缓存失效_mybatis二级缓存默认开启吗1.缓存介绍Mybatis提供查询缓存,如果缓存中有数据就不用从数据库中获取,用于减轻数据压力,提高系统性能。Mybatis的查询缓存总共有两级,我们称之为一级缓存和二级缓存,如图:一级缓存是SqlSession级别的缓存。在操作数据库时需要构造sqlSession对象,在对象中有一个数据结构(HashMap)用于存储缓存数据。不同的sqlSession之间的缓存数据区域(HashMap)是互相…

    2022年9月20日
    3
  • 《开源安全运维平台-OSSIM最佳实践》已经上市

    《开源安全运维平台-OSSIM最佳实践》已经上市经多年潜心研究开源技术,历时三年创作的《开源安全运维平台OSSIM最佳实践》一书即将出版。该书用100多万字记录了作者10多年的OSSIM研究应用成果,重点展示了开源安全管理平台OSSIM在大型企业网运维管理中的实践。国内目前也有各式各样的运维系统,经过笔者对比分析得出这些工具无论在功能上、性能上还是在安全和稳定性易用性上都无法跟OSSIM系统想媲美,而且很多国内的开源安全运维项目在发布几年后就逐步淡出了舞台,而OSSIM持续发展了十多年。

    2025年7月26日
    5
  • 使用cdn实现免备案_阿里云备案的域名可以在腾讯云用吗

    使用cdn实现免备案_阿里云备案的域名可以在腾讯云用吗昨晚在QQ群看到的,目前可以白嫖!无视阿里云给你分配的cname地址域名直接解析到发的那些cname地址上面就可以使用了!添加CDN,加速区域选择“全球(不含中国大陆)”,然后把域名解析到阿里云国内CDN节点IP上。注意:知道的人多了或者也许会失效!这是我的网站测试的结果可以访问看一下速度:爱云影视…

    2025年10月23日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号