我有一些代码在其中使用 dispatch_semaphore_t 来指示操作完成。当信号量是成员变量时,它的行为似乎不正确。我将展示有效的示例代码和一个似乎无效的示例:
@implementation someClass
{
dispatch_semaphore_t memberSem;
dispatch_semaphore_t* semPtr;
NSThread* worker;
BOOL taskDone;
}
- (id)init
{
// Set up the worker thread and launch it - not shown here.
memberSem= dispatch_semaphore_create(0);
semPtr= NULL;
taskDone= FALSE;
}
- (void)dealloc
{
// Clean up the worker thread as needed - not shown here.
if((NULL != semPtr) && (NULL != *semPtr))
disptatch_release(*semPtr);
dispatch_release(memberSem);
}
- (void)doSomethingArduous
{
while([self notDone]) // Does something like check a limit.
[self doIt]; // Does something like process data and increment a counter.
taskDone= TRUE; // I know this should be protected, but keeping the example simple for now.
if((NULL != semPtr) && (NULL != *semPtr))
dispatch_semaphore_signal(*semPtr); // I will put a breakpoint here, call it "SIGNAL"
}
- (BOOL)getSomethingDoneUseLocalSemaphore
{
taskDone= FALSE; // I know this should be protected, but keeping the example simple for now.
dispatch_semaphore_t localSem= dispatch_semaphore_create(0);
semPtr= &localSem;
[self performSelector:doSomethingArduous onThread:worker withObject:nil waitUntilDone:NO];
dispatch_time_t timeUp= dispatch_time(DISPATCH_TIME_NOW, (uint64_t)(2.5 * NSEC_PER_SEC));
dispatch_semaphore_wait(localSem, timeUp);
semPtr= NULL;
dispatch_release(localSem);
// I know I could just return taskDone. The example is this way to show what the problem is.
if(taskDone) // Again with thread safety.
return TRUE;
return FALSE;
}
- (BOOL)getSomethingDoneUseMemberSemaphore
{
taskDone= FALSE; // I know this should be protected, but keeping the example simple for now.
semPtr= &memberSem; // I will put a breakpoint here, call it "START"
[self performSelector:doSomethingArduous onThread:worker withObject:nil waitUntilDone:NO];
dispatch_time_t timeUp= dispatch_time(DISPATCH_TIME_NOW, (uint64_t)(2.5 * NSEC_PER_SEC));
dispatch_semaphore_wait(memberSem, timeUp);
semPtr= NULL;
// I know I could just return taskDone. The example is this way to show what the problem is.
if(taskDone) // Again with thread safety.
return TRUE; // I will put a breakpoint here, call it "TASK_DONE"
return FALSE; // I will put a breakpoint here, call it "TASK_NOT_DONE"
}
- (void)hereIsWhereWeBringItTogether
{
BOOL gotItDoneLocal= [self getSomethingDoneUseLocalSemaphore]; // Will return TRUE.
gotItDoneLocal= [self getSomethingDoneUseLocalSemaphore]; // Will return TRUE.
gotItDoneLocal= [self getSomethingDoneUseLocalSemaphore]; // Will return TRUE.
BOOL gotItDoneMember= [self getSomethingDoneUseMemberSemaphore]; // Will return TRUE. I will put a breakpoint here, call it "RUN_TEST"
gotItDoneMember= [self getSomethingDoneUseMemberSemaphore]; // Will return FALSE.
}
因此,鉴于该代码和我得到/得到的结果,我按照我的真实代码中的描述放置了断点:一个在主函数中,一个在工作函数中开始,一个在成员信号量发出信号的地方,以及等待后两个。
我发现在我使用成员信号量的情况下,在第一轮我停在断点“RUN_TEST”,运行并命中断点“START”,运行然后命中断点“SIGNAL”,运行然后命中断点“TASK_DONE”——一切如预期。
当我继续运行时,我遇到断点“START”,运行然后遇到断点“TASK_NOT_DONE”,运行然后遇到断点“SIGNAL”
也就是说,当我使用作为成员的信号量运行序列,并执行看起来正确的信号/等待时,我第二次尝试等待那个信号量时,我似乎错过了它,它在我发出信号后收到信号已经退出等待。
我似乎要么没有管理计数权(信号/等待配对),要么该成员信号量不会返回到未发出信号的状态。
我觉得这里缺少一些基本的东西。任何输入将不胜感激。
编辑:最终我似乎遗漏了什么是因为我的实际代码有点复杂。不是从艰巨的任务中干净利落地返回,而是涉及多个线程和一个 postNotification。我用通知处理程序中的代码替换了 postNotification - 它设置了一个标志并向信号量发出信号。这样就消除了通知处理程序可能引入的任何延迟。
最佳答案
是的,这是预期的行为。如果您等待信号超时,当信号到来时,next 调用 dispatch_semaphore_wait
将捕获该信号量。考虑以下示例:
例如:
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
dispatch_time_t timeout;
// in 5 seconds, issue signal
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
sleep(5);
NSLog(@"Signal 1");
dispatch_semaphore_signal(semaphore);
});
// wait four seconds for signal (i.e. we're going to time out before the signal)
NSLog(@"Waiting 1");
timeout = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4.0 * NSEC_PER_SEC));
if (dispatch_semaphore_wait(semaphore, timeout))
NSLog(@"Waiting for 1: timed out");
else
NSLog(@"Waiting for 1: caught signal");
// now, let's issue a second signal in another five seconds
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
sleep(5);
NSLog(@"Signal 2");
dispatch_semaphore_signal(semaphore);
});
// wait another four seconds for signal
// this time we're not going to time out waiting for the second signal,
// because we'll actually catch that first signal, "signal 1")
NSLog(@"Waiting 2");
timeout = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4.0 * NSEC_PER_SEC));
if (dispatch_semaphore_wait(semaphore, timeout))
NSLog(@"Waiting for 2: timed out");
else
NSLog(@"Waiting for 2: caught signal");
// note, "signal 2" is still forthcoming and the above code's
// signals and waits are unbalanced
因此,当您使用类实例变量时,您的 getSomethingDoneUseMemberSemaphore
的行为与上面类似,其中对 dispatch_semaphore_wait
的第二次调用将捕获发出的第一个信号,因为 (a) 它是同一个信号量; (b) 如果第一次调用 dispatch_semaphore_signal
超时。
但是如果你每次都使用唯一的信号量,那么第二次调用dispatch_semaphore_wait
将不会响应第一个信号量的dispatch_semaphore_signal
。
关于objective-c - dispatch_semaphore_t 重用-我在这里缺少什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18363936/