I was trying out a sample thread program from google and i am getting a runtime exception.
Is there any website that gives an example of how to use runloops along with threads.
I need to set two events and spawn a thread and to do another function parallely.
// Runner.m
#import "Runner.h"
@implementation Runner
- (void)rumMe:(id)ignored {
NSLog(@"Running with threads!!");
}
@end
// Runner.h
@interface Runner : NSObject
-(void)rumMe:(id)ignored;
@end
// Thread1.m
#import <Foundation/Foundation.h>
#import "Runner.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Runner* runner = [Runner new];
[NSThread detachNewThreadSelector:@selector(runMe:) toTarget:runner withObject:nil];
[pool drain];
return 0;
}
Runtime Exception:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '***
-[NSThread initWithTarget:selector:object:]: target does not implement selector (***
-[Runner runMe:])'
First part: you had a typo
Second part: your main function is returning and causing the program to exit before you have given the thread a chance to do anything. In this simple simple example, you could simply
right after the call to
detachNewThreadSelector:In more complex cases, you might need to make a call to
CFRunLoopRun();on the main thread, or take other action to keep the second thread alive.