Use grad Central Dispatch to simplify iPhone Development

Source: Internet
Author: User

From http://www.cnblogs.com/vinceoniphone/archive/2011/04/08/2009297.html

 

GCD Overview

1. GCD included in libsystem. dylib

2. Available for allProgramUse.

-# Include <dispatch/dispatch. h>

3. gcd api provides block-based and function-based variants

-Currently, only block-based APIs are provided.

GCD Summary

1. Blocks

-Dispatch_async ()

2. Queues

-Lightweight list of Blocks

-Enqueue/dequeue is FIFO

3. dispatch_get_main_queue ()

-Main thread/main runloop

4. dispatch_queue_create ()

-Automatic helper thread

Benefits of GCD

1. Efficient-more CPU cycles available for your code

2. ease of use

-Blocks are easy to use

-Queues are inherently producer/consumer

3. Systemwide perspective

-Only the OS can balance unrelated subsystems

Compatibility

1. Existing threading and synchronization primitives are 100% compatible

2. GCD threads are wrapped POSIX Threads

-Do not cancel, exit, kill, join, or detach GCD threads

3. GCD reuses threads

-Restore any per-thread state changed within a block

Multithreading

Session 206 has mentioned this content.

Lock Resources

1. mutex access to key resources.

2. Access shared resources in sequence in the thread.

3. ensure data integrity

No GCDCode

-(Void) updateimagecachewithimg :( uiimage *) IMG {

Nslock * l = self. imagecachelock;

[L lock];

// Critical section

If ([self. imagecache containsobj: img]) {

[L unlock]; // don't forget to unlock

Return;

}

[Self. imagecache addobj: img];

[L unlock];

}

GCD code

-(Void) updateimagecachewithimg :( nsimage *) IMG {

Dispatch_queue_t queue = self. imagecachequeue;

Dispatch_sync (queue, ^ {// you can change dispatch_sync to dispatch_async to defer critical section

// Critical section

If ([self. imagecache containsobj: img]) {

Return;

}

[Self. imagecache addobj: img];

});

}

Inter-thread Communication

Use the following method (without gcd)

-Receivmselecdomainmainthread: withobject: waituntildone:

-Receivmselector: onthread: withobject: waituntildone:

-Receivmselector: withobject: afterdelay:

-Optional mselectorinbackground: withobject:

GCD code, equivalentPerformselector: onthread: withobject: waituntildone:

// Waituntildone: No

Dispatch_async (queue, ^ {

[Myobject dosomething: Foo withdata: bar];

});

// Waituntildone: Yes

Dispatch_sync (queue, ^ {

[Myobject dosomething: Foo withdata: bar];

});

GCD code, equivalent to javasmselector: withobject: afterdelay:Dispatch_time_t delay; delay = dispatch_time (dispatch_time_now, 50000/* 50 μs */); dispatch_after (delay, queue, ^ {[myobject dosomething: Foo withdata: bar];})The GCD code is equivalent to javasmselectorinbackground: withobject:Dispatch_queue_t queue = dispatch_get_global_queue (0, 0); dispatch_async (queue, ^ {[myobject dosomething: Foo withdata: bar];});
Global queues1. enqueue/dequeue is FIFO

2. Concurrent execution-non-FIFO completion order

3. dispatch_get_global_queue (priority, 0)

4. Global queues map GCD activity to real threads 5. Priority bands-dispatch_queue_priority_high-dispatch_queue_priority_default-dispatch_queue_priority_low
The following three sections describe a lot of content, but I have not understood it yet. If you are interested, watch the original video. -Dispatch sources
-Source cancellation-target queues
GCD object

GCD method Overview

Manage object Lifecycle

1. Ensure objects captured by blocks are valid when blocks are executed

-Objects must be retained and released around asynchronous operations

2. Objective-C objects captured by blocks are auto-retained and auto-released

3. Other Objects captured by blocks must be retained by your code

-Cfretain ()/cfrelease ()

-Dispatch_retain ()/dispatch_release ()

Pending and restoring

1. Suspend and resume only affects queues and sources you create

-Sources are created suspended

2. suspension is asynchronous

-Takes Effect Between Blocks

3. Your queues can predictably suspend objects that target them

Program Context

1. Applications can attach custom data to GCD objects

-Dispatch_set_context ()/dispatch_get_context ()

2. Optional finalizer callback

-Dispatch_set_finalizer_f ()

-Allows attached context to be freed with object

-Called on the target queue of the object

Wwdc2010 session211 simplifying iPhone app development with Grand Central Dispatch

Daniel Steffen-core OS

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.