iOS下用sem_init初始化信号量总是返回-1

本文详细介绍了在iOS开发中遇到创建无名信号量导致的初始化错误问题,并提供了解决方案。通过使用有名的信号量,可以避免错误并确保程序正常运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

总是返回-1说明初始化错误,原因是由于iOS不支持创建无名的信号量所至。

解决方案是造建有名的信号量。

代码如下:

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <assert.h>
#include <semaphore.h>

sem_t * CreateSemaphore( const char * inName, const int inStartingCount );
bool DestroySemaphore( sem_t * inSemaphore );
void SignalSemaphore( sem_t * inSemaphore );
void WaitSemaphore( sem_t * inSemaphore );
bool TryWaitSemaphore( sem_t * inSemaphore );
bool ClearSemaphore( const char * inName);

//
sem_t * CreateSemaphore( const char * inName, const int inStartingCount )
{
	sem_t * semaphore = sem_open( inName, O_CREAT, 0644, inStartingCount );
    
	//
	if( semaphore == SEM_FAILED )
	{
		switch( errno )
		{
            case EEXIST:
                printf( "Semaphore with name '%s' already exists.\n", inName );
                break;
                
            default:
                printf( "Unhandled error: %d.\n", errno );
                break;
		}
        
		//
		assert(false);
		return SEM_FAILED;
	}
    
	//
	return semaphore;
}


//
bool DestroySemaphore( sem_t * inSemaphore )
{
	int retErr = sem_close( inSemaphore );
    
	//
	if( retErr == -1 )
	{
		//
		switch( errno )
		{
            case EINVAL:
                printf( "inSemaphore is not a valid sem_t object." );
                break;
                
            default:
                printf( "Unhandled error: %d.\n", errno );
                break;
		}
        
		//
		assert(false);
		return false;
	}
    
	//
	return true;
}

//
void SignalSemaphore( sem_t * inSemaphore )
{
	sem_post( inSemaphore );
}

//
void WaitSemaphore( sem_t * inSemaphore )
{
	sem_wait( inSemaphore );
}

//
bool TryWaitSemaphore( sem_t * inSemaphore )
{
	int retErr = sem_trywait( inSemaphore );
    
	//
	if( retErr == -1 )
	{
		//
		if( errno != EAGAIN )
		{
			printf( "Unhandled error: %d\n", errno );
			assert( false );
		}
        
		//
		return false;
	}
    
	//
	return true;
}

bool ClearSemaphore( const char * inName)
{
    int retErr = sem_unlink(inName);
    
    if (retErr == -1) {
        
        return false;
        
    }
    
    return true;
}


使用完后还要记得

//使用sem_open方式创建的信号量在使用完毕需清除.
ClearSemaphore("mysem");




Reference:

http://getoffmylawnentertainment.com/blog/2011/06/27/sem_init-function-not-implemented/

http://www.lastrayofhope.com/2011/03/15/ios-semaphore-problems/

Ld /Users/qingguo/Library/Developer/Xcode/DerivedData/APP_FFT_Framework-fjxfxozmofjhqzbflrggfayhgspf/Build/Products/Debug-iphoneos/APP_FFT_Framework.framework/APP_FFT_Framework normal (in target 'APP_FFT_Framework' from project 'APP_FFT_Framework') cd /Users/qingguo/Desktop/nounou/yes/QGtest/APP_FFT_Framework /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -Xlinker -reproducible -target arm64-apple-ios18.2 -dynamiclib -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS18.2.sdk -O0 -L/Users/qingguo/Library/Developer/Xcode/DerivedData/APP_FFT_Framework-fjxfxozmofjhqzbflrggfayhgspf/Build/Intermediates.noindex/EagerLinkingTBDs/Debug-iphoneos -L/Users/qingguo/Library/Developer/Xcode/DerivedData/APP_FFT_Framework-fjxfxozmofjhqzbflrggfayhgspf/Build/Products/Debug-iphoneos -L/opt/homebrew/lib -F/Users/qingguo/Library/Developer/Xcode/DerivedData/APP_FFT_Framework-fjxfxozmofjhqzbflrggfayhgspf/Build/Intermediates.noindex/EagerLinkingTBDs/Debug-iphoneos -F/Users/qingguo/Library/Developer/Xcode/DerivedData/APP_FFT_Framework-fjxfxozmofjhqzbflrggfayhgspf/Build/Products/Debug-iphoneos -filelist /Users/qingguo/Library/Developer/Xcode/DerivedData/APP_FFT_Framework-fjxfxozmofjhqzbflrggfayhgspf/Build/Intermediates.noindex/APP_FFT_Framework.build/Debug-iphoneos/APP_FFT_Framework.build/Objects-normal/arm64/APP_FFT_Framework.LinkFileList -install_name @rpath/APP_FFT_Framework.framework/APP_FFT_Framework -Xlinker -rpath -Xlinker @executable_path/Frameworks -Xlinker -rpath -Xlinker @loader_path/Frameworks -dead_strip -Xlinker -object_path_lto -Xlinker /Users/qingguo/Library/Developer/Xcode/DerivedData/APP_FFT_Framework-fjxfxozmofjhqzbflrggfayhgspf/Build/Intermediates.noindex/APP_FFT_Framework.build/Debug-iphoneos/APP_FFT_Framework.build/Objects-normal/arm64/APP_FFT_Framework_lto.o -Xlinker -export_dynamic -Xlinker -no_deduplicate -L/opt/homebrew/lib\ -lgsl\ -lgslcblas -compatibility_version 1 -current_version 1 -Xlinker -dependency_info -Xlinker /Users/qingguo/Library/Developer/Xcode/DerivedData/APP_FFT_Framework-fjxfxozmofjhqzbflrggfayhgspf/Build/Intermediates.noindex/APP_FFT_Framework.build/Debug-iphoneos/APP_FFT_Framework.build/Objects-normal/arm64/APP_FFT_Framework_dependency_info.dat -o /Users/qingguo/Library/Developer/Xcode/DerivedData/APP_FFT_Framework-fjxfxozmofjhqzbflrggfayhgspf/Build/Products/Debug-iphoneos/APP_FFT_Framework.framework/APP_FFT_Framework ld: warning: search path '/opt/homebrew/lib -lgsl -lgslcblas' not found Undefined symbols for architecture arm64: "_gsl_interp_cspline_periodic", referenced from: _test_csplinep in test.o _test_csplinep2 in test.o "_gsl_spline_eval_deriv", referenced from: _bicubic_init in bicubic.o _bicubic_init in bicubic.o _bicubic_init in bicubic.o "_gsl_vector_alloc", referenced from: _bicubic_init in bicubic.o _bicubic_init in bicubic.o _bicubic_init in bicubic.o _bicubic_init in bicubic.o _bicubic_init in bicubic.o _bicubic_init in bicubic.o "_gsl_vector_free", referenced from: _bicubic_init in bicubic.o _bicubic_init in bicubic.o _bicubic_init in bicubic.o _bicubic_init in bicubic.o _bicubic_init in bicubic.o _bicubic_init in bicubic.o "_gsl_vector_set", referenced from: _bicubic_init in bicubic.o _bicubic_init in bicubic.o _bicubic_init in bicubic.o _bicubic_init in bicubic.o _bicubic_init in bicubic.o _bicubic_init in bicubic.o "_gsl_vector_view_array", referenced from: _cspline_init in cspline.o _cspline_init in cspline.o _cspline_init in cspline.o _cspline_init in cspline.o ld: symbol(s) not found for architecture arm64 clang++: error: linker command failed with exit code 1 (use -v to see invocation)
最新发布
03-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值