Is NSNotificationCenter thread safe?
Asked Answered
J

3

15

Can I post a notification in a given queue and receive it on another? I want to use notifications to communicate different queues, but I'm not sure if this is safe...

Jasik answered 12/4, 2013 at 10:1 Comment(2)
you can use it, but there are some advances solutions to synchronise the different threads as well like semaphores, locks etc...Nehru
It sounds like your essentially asking the same question as I answered here.Cusick
C
19

No. Notifications are delivered in the same thread that they are sent from, this you will need to re-send it in some way to get the notification to your thread.

Corey answered 12/4, 2013 at 10:25 Comment(0)
L
15

No.

Apple's docs on the subject say: "Regular notification centers deliver notifications on the thread in which the notification was posted. [...] At times, you may require notifications to be delivered on a particular thread that is determined by you instead of the notification center. [...] In these cases, you must capture the notifications as they are delivered on the default thread and redirect them to the appropriate thread."

The following documentation from Apple might help: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Notifications/Articles/Threading.html#//apple_ref/doc/uid/20001289-CEGJFDFG

Liszt answered 12/4, 2013 at 10:27 Comment(0)
D
0

The question asked in the title of this page is “Is NSNotificationCenter thread safe?” The answer to that question is YES. From “Thread Safety Summary” in the Threaded Programming Guide:

The following classes and functions are generally considered to be thread-safe. You can use the same instance from multiple threads without first acquiring a lock.

[ … ]
NSNotification
NSNotificationCenter

The question asked in the body of this page has been answered correctly by other responders.

Dewittdewlap answered 19/4, 2024 at 14:31 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.