Create two threads in Objective-C Create two threads in Objective-C multithreading multithreading

Create two threads in Objective-C


Use dispatch queues. They're essentially lightweight threads for which you don't need to worry about the threading or queueing directly.

-(void) spawn{    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{        [self doWorkInBackground];    });}

You can use one of the built-in queues or your own.

And you should probably read up on blocks too, in particular the memory management aspect.


- (void)performBlockInBackground:(void (^)())block {    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{        block();    });}

Then you call it like this:

[self performBlockInBackground:^{    NSLog(@"Log from background");}];


This is a pretty deep discussion, but Apple's "Concurrency Programming Guide" should get you started.