开发者

NSNotification race condition

开发者 https://www.devze.com 2022-12-24 09:24 出处:网络
Are there any race condition issues when using NSNotifications within a single thread? Here is a sample method:

Are there any race condition issues when using NSNotifications within a single thread? Here is a sample method:

- (void) playerTog开发者_StackOverflow中文版gled: (NSNotification *) notification {
if (timerPane.playing && ! timerPane.paused) {
    [playerPane toggleCurrentPlayer];
    [timerPane toggleTimer];
    [mainPane playerToggled];
}

}

The first two calls after the condition will trigger NSNotifications that will be received by mainPane. Is mainPane guaranteed to receive the playerToggled message after those notifications? I should say that this code seems to work as desired (playerToggled always executes last). But I'm not sure what timing issues there are around notifications and I can't find a specific answer.


There are no race conditions to be expected. In addition to Dan Donaldson's answer, here is another quote from the docs for 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.


I am not exactly sure what you mean, but I think this will be helpful to you:

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

Especially this part: Using the NSNotificationCenter’s postNotification: method and its variants, you can post a notification to a notification center. However, the invocation of the method is synchronous: before the posting object can resume its thread of execution, it must wait until the notification center dispatches the notification to all observers and returns.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号