Friday, 17 May 2013

Multi-threading

Grand Central Dispatch (GCD) is a C API.  The basic idea is that you have queues of operations, which are specified using blocks. The blocks in the queue run in another thread except main thread.

There are two types of queues.  Serial Queues and Concurrent Queues
In serial queues blocks are executed in order. (one by one)

When do we use Multi-threading:

Accessing the network
Big calculation, hugs loines of code


Creating and releasing queues:
dispatch_queue_t dishpatch_queue_create(const char *label, NULL); // Null means serial queue

Putting blocks in the queue:
typedef void (^dispatch_block_t)(void);
void dispatch_async(dispatch_queue_t queue, dispatch_block_t block); //it puts the block in the queue then returns to main thread immediately

void dispatch_sync(dispatch_queue_t queue, dispatch_block_t block);  // puts the block in the queue and waits until the block finally gets to run to compelition and get backs to current thread

Getting the current or main queue
dispatch_queue_t dispatch_get_current_queue();
dispatch_queue_t dispatch_get_main_queue();


written by: infinitywebseo 

1 comment: