Creating Multithreading with GCD

Source: Internet
Author: User
Tags gcd

GCD is the recommended multithreaded solution for Apple, and the typical scenario is when a program needs to do complex, time-consuming calculations or operations. such as sending network requests, downloading large pictures and so on. If this is done by the main thread, the main thread will not be able to respond to the user's interface operation, which greatly affects the user experience.

At this point, it becomes necessary to hand over these time-consuming tasks to the child threads. GCD is a set of libraries written in C language.

- (void) viewdidload{[Super Viewdidload]; Nsassert (_image,@"Image not set; required to use View Controller"); Self.photoImageView.image=_image; //Resize if neccessary to ensure it 's not pixelated    if(_image.size.height <= self.photoImageView.bounds.size.height &&_image.size.width<=self.photoImageView.bounds.size.width) {[Self.photoimageview setcontentmode:uiviewcontentmodecenter]; } dispatch_async (Dispatch_get_global_queue (Dispatch_queue_priority_high,0), ^{//1UIImage *overlayimage =[self faceoverlayimagefromimage:_image]; Dispatch_async (Dispatch_get_main_queue (),^{//2[Self fadeinnewimage:overlayimage];//3        }); });}

As the above code shows, we usually have a complex task with a child thread ~ that is:

0)


Dispatch_async to create an asynchronously executed task, Dispatch_get_global_queue (Dispatch_queue_priority_high, 0) is then placed in the global queue, which is the parallel queue of a system. In this way, the code executes in the child thread and guarantees that the thread will run smoothly.

If you use a live queue, the result is that it becomes asynchronous and you don't get the effect you want. Finally, the main thread will perform the task. So when it comes to multithreading, we choose to execute asynchronously in parallel queues.

And when it comes to updating the interface, it's important to note that when you need to access Uikit, you have to do it in the main thread, so you need to use:

// 2            // 3        });

The above code to go back to the main thread, complete the interface related operations. Whenever you need to update the interface, remember to get live queue ~ ~ As to, if this place, we use is dispatch_sync back then back to the main thread, what will be the answer is to block the main thread. Because the main thread is executing the task at all times, and we suddenly join a synchronously executed task into the live queue, the main thread is executing the task, and this plus task waits for each other, causing a deadlock. ~ ~ So anyway, it must be remembered that the master queue/serial queue cannot be used to perform synchronously!!

Dispatch_syn (Dispatch_get_main_queue (), ^{
NSLog (@ "3");
Deadlock Reason
1:dispatch_sync is waiting for the block statement to complete, and the block statement needs to be executed in the main thread, so Dispatch_sync will cause a deadlock if it is called on the main thread.
The 2:dispatch_sync is synchronous, which in itself blocks the current thread, or the main thread. and a block is plugged into the main thread, so a deadlock occurs.
//});

Multithreading related Good article: https://github.com/nixzhu/dev-blog/blob/master/2014-04-19-grand-central-dispatch-in-depth-part-1.md
Http://www.jianshu.com/p/0b0d9b1f1f19
         http://www.jianshu.com/p/d3f954582231

Creating Multithreading with GCD

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.