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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • matlab计算基尼系数并分解_matlab斐波那契数列的第n项

    matlab计算基尼系数并分解_matlab斐波那契数列的第n项前两天想偷个懒在网上找了好久关于求基尼系数的现成公式,结果很令人失望,于是只好奋发图强自己动手啦。​开始之前还是先脑补一些什么是基尼系数?—————————————————————————————-​居民收入分配的差异程度,是当前人们所普遍关心的一个问题。收入分配差异的合理与否,一方…

    2022年10月13日
    0
  • Java代码生成器[通俗易懂]

    Java代码生成器[通俗易懂]项目说明本项目基于是基于renren-generator定制的代码生成器文章目录**项目说明**不同点:效果原理分析如何定制开发?更多可能存在的坑代码地址不同点:因为本人的公司使用的是tkmyabtis+swagger构建restapi,而renren-generator用的是mybatis-plus,而且不支持swagger,所以有了本项目效果…

    2022年4月27日
    50
  • ZigBee协议栈简介

    ZigBee协议栈简介文章目录Zigbee协议栈简介如何理解Zigbee协议栈如何使用Zigbee协议栈Zigbee协议栈简介  Zigbee协议分为2部分:IEEE802.15.4定义了PHY(物理层)和MAC(介质访问层)技术规范。Zigbee联盟定义了NWK(网络层)、APS(应用程序支持层)、APL(应用层)技术规范。  Zigbee协议栈就是将各个层定义的协议都集合在一起,以函数的形式实现,并给用户提供API,用户可以直接调用。如何理解Zigbee协议栈  TI推出的ZigBee2007协议栈也

    2022年5月8日
    36
  • StretchBlt用法[通俗易懂]

    StretchBlt用法[通俗易懂]首先定义protected: BITMAPbmp;其次实现///////////////////////////////////////////////////////////////////////////////CExamineViewdrawingvoidCExamineView::OnDraw(CDC*pDC){ CExamineDoc*pDoc=GetDo

    2025年6月13日
    0
  • win10系统下JDK环境变量配置失败的原因

    win10系统下JDK环境变量配置失败的原因win10系统下配置JDK环境变量,按照网上的教程,最后cmd验证总是出错,经过探索才发现,网上的教程都是win7的,在配置PATH时,网上的教程是:%JAVA_HOME%\bin;最后的分号,win7需要,win10是不需要的。当点击编辑出现的是这个界面的时候,直接添加即可,不用带;…

    2022年7月21日
    9
  • linux内核编程指南_UNIX/LINUX

    linux内核编程指南_UNIX/LINUX3.3 Linux内核的组成3.3.1 Linux内核源代码的目录结构Linux内核源代码包含如下目录。arch:包含和硬件体系结构相关的代码,每种平台占一个相应的目录,如i386、arm、arm64、powerpc、mips等。Linux内核目前已经支持30种左右的体系结构。在arch目录下,存放的是各个平台以及各个平台的芯片对Linux内核进程调度、内存管理、中断等的支持,以及每个具体的SoC…

    2022年9月13日
    0

发表回复

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

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