Creating a simple thread

suggest change

The 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.
    }
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents