Objective-C Language Create more complex thread


Example

Using a subclass of NSThread allows implementation of more complex threads (for example, to allow passing more arguments or to encapsulate all related helper methods in one class). Additionally, the NSThread instance can be saved in a property or variable and can be queried about its current state (whether it's still running).

The NSThread class supports a method called cancel that can be called from any thread, which then sets the cancelled property to YES in a thread-safe way. The thread implementation can query (and/or observe) the cancelled property and exit its main method. This can be used to gracefully shut down a worker thread.

// Create a new NSThread subclass
@interface MyThread : NSThread

// Add properties for values that need to be passed from the caller to the new
// thread. Caller must not modify these once the thread is started to avoid
// threading issues (or the properties must be made thread-safe using locks).
@property NSInteger someProperty;

@end

@implementation MyThread

- (void)main
{
    @autoreleasepool {
        // The main thread method goes here
        NSLog(@"New thread. Some property: %ld", (long)self.someProperty);
    }
}

@end


MyThread *thread = [[MyThread alloc] init];
thread.someProperty = 42;
[thread start];