iphone - NSNotification 竞争条件

标签 iphone cocoa race-condition nsnotifications

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

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

}

条件后的前两个调用将触发 mainPane 接收的 NSNotifications。 mainPane 是否保证在这些通知后收到playerToggled 消息?我应该说这段代码似乎按预期工作(playerToggled 总是最后执行)。但我不确定通知存在哪些时间问题,也找不到具体的答案。

最佳答案

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

A notification center delivers notifications to observers synchronously. In other words, the postNotification: methods do not return until all observers have received and processed the notification. To send notifications asynchronously use NSNotificationQueue.

关于iphone - NSNotification 竞争条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2510704/

相关文章:

java - 在单个后台线程定期修改 map 的同时读取 map

java - 这个例子中有竞争条件吗?如果是这样,如何避免?

iPhone 横向 View 屏幕截图

objective-c - 使用 Cocoa 在 Objective-C 的事件处理程序中移除本地事件监视器的竞争条件

iphone - Objective-C 中向数组添加数据的问题

objective-c - 在 OS X 上利用单独应用程序的资源

objective-c - 需要从同一方法中调用的完成 block 返回数据

objective-c - NSTableview 改变高亮颜色

iphone - UISearchDisplayController "No Results"文本

iphone - 异步图片加载错误