Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to enter Cocoa multithreaded mode without creating fake NSThread?

Apple Threading guide says:

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.

In my iOS app, I'm starting several pthreads from C++ code right from the start. To be sure the app behaves right, according to the doc above, I create a fake NSThread that does nothing. I don't like creating such useless code (usually it's WTF, when you first read it) and I want to avoid doing so. Is there any better way to put my app into multithreaded mode?

like image 474
fspirit Avatar asked May 06 '11 08:05

fspirit


1 Answers

If there is, it's not public and might be unstable.

If you're hitting a WTF in your code, rename and rejigger things so that it makes sense. Since you need a dummy object with a dummy selector as well, you could just add a throwaway class like CocoaMultithreading and then send it a +beginMultithreading message:

@interface CocoaMultithreading : NSObject
+ (void)beginMultithreading;
@end

int
main(void) {
    [CocoaMultithreading beginMultithreading];
    /* now do whatever you want */
    return EXIT_SUCCESS;
}

@implementation CocoaMultithreading
+ (void)dummyThread:(id)unused
{
    (void)unused;
}

+ (void)beginMultithreading
{
    [NSThread detachNewThreadSelector:@selector(dummyThread:)
            toTarget:self withObject:nil];
}
@end

That should be explicit enough.

ETA: Alexander Staubo points out that, since OS X 10.5/iOS 2.0, you can call the -start method on an NSThread directly, so the very simplest way to flip on Cocoa multithreading would be this:

void XXXActivateCocoaMultithreading(void) { [[NSThread new] start]; }

Then, in your main function:

XXXActivateCocoaMultithreading();

That is explicit, as well, but far less messy. (The XXX is there to remind you to prefix non-static functions. Since static functions often become non-static at some point, prefixing them all from the start is a good move.)

like image 63
Jeremy W. Sherman Avatar answered Oct 10 '22 03:10

Jeremy W. Sherman