Multi-Threading
What mechanisms does iOS provide to support multi-threading?
NSThread
creates a new low-level thread which can be started by calling thestart
method.NSThread* myThread = [[NSThread alloc] initWithTarget:self selector:@selector(myThreadMainMethod:) object:nil]; [myThread start];
NSOperationQueue
allows a pool of threads to be created and used to executeNSOperation
s in parallel.NSOperation
s can also be run on the main thread by askingNSOperationQueue
for themainQueue
.NSOperationQueue* myQueue = [[NSOperationQueue alloc] init]; [myQueue addOperation:anOperation]; [myQueue addOperationWithBlock:^{ /* Do something. */ }];
- GCD or Grand Central Dispatch is a modern feature of Objective-C that provides a rich set of methods and API's to use in order to support common multi-threading tasks. GCD provides a way to queue tasks for dispatch on either the main thread, a concurrent queue (tasks are run in parallel) or a serial queue (tasks are run in FIFO order).
dispatch_queue_t myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(myQueue, ^{ printf("Do some work here.\n"); });
This comment has been removed by the author.
ReplyDelete