The most simple way to create a thread is by calling a selector "in the background". This means a new thread is created to execute the selector. The receiving object can be any object, not just self
, but it needs to respond to the given selector.
- (void)createThread {
[self performSelectorInBackground:@selector(threadMainWithOptionalArgument:)
withObject:someObject];
}
- (void)threadMainWithOptionalArgument:(id)argument {
// To avoid memory leaks, the first thing a thread method needs to do is
// create a new autorelease pool, either manually or via "@autoreleasepool".
@autoreleasepool {
// The thread code should be here.
}
}