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


相关推荐

  • Pyinstaller打包exe完整教程

    Pyinstaller打包exe完整教程原创文|Space9Python文件打包成可安装、无需Python依赖的高效可执行exe程序工具及环境PyInstallerInnoSetupWindows和PythonPyInstaller打包Python应用程序为独立的可执行文件安装PyInstallerpypi镜像使用帮助https://mirrors.tuna.tsinghua.edu.cn/help/pypi/pipinstall-ihttps://pypi.tuna.tsinghua.edu.cn/simple

    2022年6月25日
    33
  • Mysql和Hash作为数据库索引的优略

    Mysql和Hash作为数据库索引的优略1.hash表只能匹配是否相等,不能实现范围查找select * from xx where id > 23; 这时就没办法索引了2.当需要按照索引进行order by时,hash值没办法支持排序select * from xx order by score desc;如果score为建立索引的字段,hash值没办法辅助排序。3.组合索引可以支持部分索引查询,如(a,b,c)的组合索引,查询中只用到了阿和b也可以查询的,如果使用hash表,组合索引会将几个字段合并hash,没办法支持部分索引

    2022年8月9日
    7
  • SQL Server 2019基础配置

    SQL Server 2019基础配置SQLServer2019基础配置1、在开始菜单中选中安装的SQLServer2019配置管理器,打开。2、点击SQLServer网络配置->MSSQLSERVER的协议->启用TCP/IP协议。示例:3、启动管理应用。示例:4、默认直接点击连接即可。示例:5、发现有如图所示的结果表示连接成功。示例:6、单击->右键->属性。示例:7…

    2022年7月20日
    17
  • 二叉树的先序,中序,后序遍历的序列_二叉树先序遍历和后序遍历正好相反

    二叉树的先序,中序,后序遍历的序列_二叉树先序遍历和后序遍历正好相反    二叉树的遍历主要有三种:(1)先(根)序遍历(根左右)(2)中(根)序遍历(左根右)(3)后(根)序遍历(左右根)举个例子:先(根)序遍历(根左右):ABDHEICFJKG中(根)序遍历(左根右):DHBEIAJFKCG后(根)序遍历(左右根):HDIEBJKFGCA    以后(根)序…

    2025年11月18日
    2
  • pycharm调用anaconda数据库_库乐队为什么无法导入

    pycharm调用anaconda数据库_库乐队为什么无法导入PyCharm中导入Anaconda库打开File/Settings,选择ProjectInterpreter,点击设置,选择Add在SystemInterpreter中选择添加Anaconda3下的python.exe编译器,选择OK选择Anaconda3下的Python编译器,点击选择Apply,点击OK…

    2022年8月29日
    3
  • MySql数据库增删改查常用语句命令「建议收藏」

    MySql数据库增删改查常用语句命令「建议收藏」文章目录增删改查语句库操作表操作增删改查实例准备表插入数据修改表数据删除表数据查询表数据常见的MySQL语句命令常见MySQL字段含义增删改查语句增删改查的语句命令为:操作命令增insert删delete改update查select或者show库操作操作代码创建数据库createdatabaseshujuku;…

    2022年5月30日
    35

发表回复

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

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