Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multithreading and Grand Central Dispatch on iOS [closed]

now i am trying to understand the concept of gcd. using grand central dispatch how to implement multithreading in my application.i have the idea about the gcd concept but i cant implement the concept to my application.i need a simple example with blocks to understand the multithreading using gcd.please help me...

like image 806
Dimil T Mohan Avatar asked Mar 04 '13 10:03

Dimil T Mohan


People also ask

Does iOS support multi threading?

Concurrency and multithreading are a core part of iOS development. Let's dive into what makes them so powerful, and how we can leverage them in our own Cocoa Touch applications. Concurrency is the notion of multiple things happening at the same time.

What mechanism does iOS provide to support multithreading?

Apple provides 2 main APIs for writing multithreaded code: Grand Central Dispatch(GCD) and Operation. Behind the scenes, they use a concept known as thread pool, meaning that those API manages a large number of threads and when a task is ready to be executed, it grabs one thread from the pool to take care of the task.

What is Grand Central Dispatch iOS?

Grand Central Dispatch (GCD) is a low-level API for managing concurrent operations. It can help improve your app's responsiveness by deferring computationally expensive tasks to the background. It's an easier concurrency model to work with than locks and threads.


2 Answers

Ok.. The most simple example )

You can write this code in any method. For example

  -(void) viewDidLoad {   
     [super viewDidLoad];  
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
       //Here your non-main thread.
       NSLog (@"Hi, I'm new thread");
       dispatch_async(dispatch_get_main_queue(), ^{
       //Here you returns to main thread.
       NSLog (@"Hi, I'm main thread");
       });
   });
}
like image 83
Arthur Avatar answered Nov 06 '22 13:11

Arthur


Try this, its very clear and easy - http://en.wikipedia.org/wiki/Grand_Central_Dispatch

like image 28
Leta0n Avatar answered Nov 06 '22 11:11

Leta0n