I tried it, but I think this notification isn't coming. Is that the normal case?
views:
44answers:
3No, this notification is sent by NSThread
. If you're using ordinary pthreads, it won't be sent.
From the docs:
Protecting the Cocoa Frameworks For multithreaded applications, Cocoa frameworks use locks and other forms of internal synchronization to ensure they behave correctly. To prevent these locks from degrading performance in the single-threaded case, however, Cocoa does not create them until the application spawns its first new thread using the NSThread class. If you spawn threads using only POSIX thread routines, Cocoa does not receive the notifications it needs to know that your application is now multithreaded. When that happens, operations involving the Cocoa frameworks may destabilize or crash your application.
To let Cocoa know that you intend to use multiple threads, all you have to do is spawn a single thread using the NSThread class and let that thread immediately exit. Your thread entry point need not do anything. Just the act of spawning a thread using NSThread is enough to ensure that the locks needed by the Cocoa frameworks are put in place.
If you are not sure if Cocoa thinks your application is multithreaded or not, you can use the isMultiThreaded method of NSThread to check.
It should also be noted that 'times have changed' since NSWillBecomeMultiThreadedNotification
was added to Foundation
. Multi-threaded programming is now much, much more common. It's now entirely within the realm of possibility, even likely, that you'll never see this notification posted in an app. Modern apps become multi-threaded very early in their life, possibly before any part of your code is ever executed. Also from the documentation:
If you are developing a Cocoa library, you can register as an observer for the
NSWillBecomeMultiThreadedNotification
if you want to be notified when the application becomes multithreaded. You should not rely on receiving this notification, though, as it might be dispatched before your library code is ever called.
I'd use [NSThread isMultiThreaded]
instead of relying on NSWillBecomeMultiThreadedNotification
.