对 thread.semaphore 中的一段源码有疑惑,请指教

By terrorist at 2022-01-12 • 0人收藏 • 744人看过
this.handle  =  CreateSemaphore(null,initCount,max,semaphoreName); 
if(! this.handle ) {
	if( targ1== "string") {	
		this.handle  =  OpenSemaphore( 0x1F0003/*_EVENT_ALL_ACCESS*/,false ,semaphoreName);
		if( this.handle ) this.conflict = "信号量已存在";
	}
}

为什么 CreateSemaphore 失败后还要 OpenSemaphore ?

3 个回复 | 最后更新于 2022-01-06
2022-01-13   #1

CreateSemaphore失败的原因有许多种,其中一种是同名的semaphoreName信号量已存在, OpenSemaphore 就是检验是不是这个原因

2022-01-13   #2

根据 https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createsemaphorea


CreateSemaphore  的返回值有三种情况:


1 如果函数成功,返回值是一个指向semaphore对象的句柄。

2 如果在调用该函数之前,命名的信号灯对象已经存在,该函数将返回一个指向现有对象的句柄,并且GetLastError返回ERROR_ALREADY_EXISTS

3 如果该函数失败,返回值为NULL。要获得扩展的错误信息,请调用GetLastError。


同名的semaphoreName信号量已存在也不会返回 NULL

2022-01-13   #3
if( !max || max <=0 ) error("@参数2 最大计数值必须大于0");

if(initCount <= 0) error("@参数3 初始计数值必须大于0");
elseif(initCount > max) error("@参数3 初始计数值不能大于最大计数值");
elseif(initCount === null) initCount = max;

if(targ1 == "string"){
	// 尝试打开
	this.handle = OpenSemaphore(0x1F0003/*_EVENT_ALL_ACCESS*/, false, semaphoreName);
	if(this.handle){
		this.conflict = "信号量已存在"
	}
	else {
		this.handle = CreateSemaphore(null, initCount, max, semaphoreName); 
		if(this.handle){
		    this.handle = OpenSemaphore(0x1F0003/*_EVENT_ALL_ACCESS*/, false, semaphoreName);
		}
	}
}
elseif(targ1 === null){
	this.handle = CreateSemaphore(null, initCount, max, null);
}

if(!this.handle) return null, ..lasterr()

..table.gc( this,"close" );

这是修改版

登录后方可回帖

登 录
信息栏
 私人小站

本站域名

ChengXu.XYZ

投诉联系:  popdes@126.com



快速上位机开发学习,本站主要记录了学习过程中遇到的问题和解决办法及上位机代码分享

这里主要专注于学习交流和经验分享.
纯私人站,当笔记本用的,学到哪写到哪.
如果侵权,联系 Popdes@126.com

友情链接
Aardio官方
Aardio资源网


才仁机械


网站地图SiteMap

Loading...