Creating a simple thread
suggest changeThe simplest way to create a thread is to call a selector in the background. A new thread is created to execute the selector. The receiver can be any object, not just self, but it must 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.
}
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents