For reasons I won't go into (unless you are interested) I want to
start Cocoa on a thread that is not the main posix thread. I wrote
the small test program below that creates a little window with a
button in it. There is a variable 'BOOL use_thread' in the main
function. If it is 'NO', then the program runs fine -- I can can
click the button, see that events are being generated. If use_thread
= YES, then it doesn't work -- sendEvent: gets called constantly with
null NSEvent pointers.
I thought I would be able to use any pthread as the "main" Cocoa
event thread, as long as that thread called [NSApplication
sharedApplication], set up the UI, and called [NSApp run]. Is that
possible? If so, how?
static void multithread_cocoa()
{
// Put Cocoa into multithreaded mode by starting an NSThread
that does nothing.
id obj = [[NSObject alloc] init];
SEL m = @selector(isEqual:);
[NSThread detachNewThreadSelector:m toTarget:obj withObject:obj];
}