2010-03-24 47 views
2

在单个线程中使用NSNotifications时是否存在竞态条件问题?下面是一个示例方法:NSNotification竞争条件

- (void) playerToggled: (NSNotification *) notification { 
if (timerPane.playing && ! timerPane.paused) { 
    [playerPane toggleCurrentPlayer]; 
    [timerPane toggleTimer]; 
    [mainPane playerToggled]; 
} 

}

的条件后的前两个电话将触发将由mainPane接收NSNotifications。 mainPane是否保证在收到通知后收到playerToggled消息?我应该说这个代码似乎按照需要工作(playerToggled总是最后执行)。但我不确定在通知周围存在什么时间问题,我找不到具体的答案。

+0

顺便说一句,你应该真的接受你到目前为止询问的10个问题的一些答案。 –

回答

3

我不完全相信你的意思,但我认为这将有助于你:

http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/Notifications/Articles/NotificationQueues.html#//apple_ref/doc/uid/20000217

特别是这部分: 使用NSNotificationCenter的postNotification:法及其变种,您可以发布向通知中心发送通知。但是,该方法的调用是同步的:在发布对象可以恢复其执行线程之前,它必须等到通知中心将通知分派给所有观察者并返回。

5

有没有竞争条件的预期。除了Dan Donaldson的回答外,这里还有NSNotificationCenter的文档中的另一个引用:

通知中心同步向观察者发送通知。换句话说,直到所有观察者都收到并处理了通知之后,postNotification:方法才会返回。要异步发送通知,请使用NSNotificationQueue。