IOS Multithreaded GCD detail

  • 2020-11-03 22:36:25
  • OfStack

Grand Central Dispatch (GCD) is a multi-core programming solution developed by Apple.

dispatch queue is divided into the following three types:

1) Main queue running in the main thread is obtained through dispatch_get_main_queue.

#definedispatch_get_main_queue() \DISPATCH_GLOBAL_OBJECT(dispatch_queue_t, _dispatch_main_q)

It can be seen that dispatch_get_main_queue is also a kind of dispatch_queue_t.

2) Parallel queue global dispatch queue is obtained through dispatch_get_global_queue, and three dispatch queue with different priorities are created by the system. Parallel queues are executed in the same order in which they were queued.

3) Serial queue serial queues1 is generally used for sequential synchronous access. Any number of serial queues can be created, and each serial queue is concurrent.

Serial queues are useful when you want tasks to be executed in a particular order. The serial queue executes only one task at the same time. We can use serial queues instead of locks to protect Shared data. Unlike locks, a serial queue guarantees that tasks will execute in a predictable order.

serial queues is created with dispatch_queue_create, using the functions dispatch_retain and dispatch_release to increase or decrease reference counts.

Usage of GCD:


// Background execution: dispatch_async(dispatch_get_global_queue(0,0), ^{//something});

// Main thread execution: dispatch_async(dispatch_get_main_queue(), ^{//something});

//1 Secondary execution: staticdispatch_once_t onceToken;

dispatch_once(&onceToken, ^{//code to be executed once});

// delay 2 Seconds to perform: doubledelayInSeconds =2.0;

dispatch_time_t popTime= dispatch_time(DISPATCH_TIME_NOW, delayInSeconds *NSEC_PER_SEC);

dispatch_after(popTime, dispatch_get_main_queue(),^(void){//code to be executed on the main queue after delay});

// The custom dispatch_queue_tdispatch_queue_t urls_queue = dispatch_queue_create("blog.devtang.com", NULL);

dispatch_async(urls_queue,^{//your code}); dispatch_release(urls_queue);

// Consolidated results dispatch_group_t group =dispatch_group_create();

dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{// Threads that execute in parallel 1});

dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{// Threads that execute in parallel 2});

dispatch_group_notify(group, dispatch_get_global_queue(0,0), ^{// Summary results });

1 example using GCD:


dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{

NSURL* url = [NSURL URLWithString:@"http://www.baidu.com"];

NSError*error;

NSString* data = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];if(data !=nil) {

dispatch_async(dispatch_get_main_queue(),^{

NSLog(@"call back, the data is: %@", data);

});

}else{

NSLog(@"error when download:%@", error);

}

});

Another use of GCD is to allow programs to run longer in the background.

When GCD is not in use, when app is quited by pressing the home key, app has only a maximum of 5 seconds to do some saving or cleaning of the resource. But after using GCD, app has up to 10 minutes to run permanently in the background. This time can be used for cleaning up the local cache, sending statistics, etc.

Sample code to make the program run long in the background is as follows:


//AppDelegate.h file @property (assign, nonatomic) UIBackgroundTaskIdentifier backgroundUpdateTask;//AppDelegate.m file - (void)applicationDidEnterBackground:(UIApplication *)application

{

[self beingBackgroundUpdateTask];// Add code here that you need to run for a long time [self endBackgroundUpdateTask];

}- (void)beingBackgroundUpdateTask

{

self.backgroundUpdateTask= [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{

[self endBackgroundUpdateTask];

}];

}- (void)endBackgroundUpdateTask

{

[[UIApplication sharedApplication] endBackgroundTask: self.backgroundUpdateTask];

self.backgroundUpdateTask=UIBackgroundTaskInvalid;

}

Text/left hand (Jane author)
The original link: http: / / www jianshu. c584601e3bf com p / 2
The copyright belongs to the author. Please contact the author for authorization and label "The author of the brief book".

Above is the IOS multithreaded GCD data collation, the follow-up to continue to supplement relevant information, thank you for the support of this site!


Related articles: