The use of multi-threaded-GCD column

Source: Internet
Author: User
Tags gcd


#import "ViewController.h"


@interface Viewcontroller ()


@end

/**

*

1.GCD The bottom layer is also implemented using threads.

2.GCD has two ways to perform tasks

Synchronous dispatch_sync(Avoid using synchronous execution of the main thread task in the main thread for easy deadlock)

Dispatch_async Async .

parameters for 3.GCD:queue: Queues Block: Tasks

4. the difference between synchronous async:

synchronization: Execution in the current thread , sequential execution

Async: Executing on another thread, you can open a thread and use the system idle time to execute

5. deadlock in the main queue (GCD) in the primary thread (UI thread) to perform the task synchronously; The main queue is the same thing as the main thread;

6. when the home Row performs a task synchronously, it waits for the previous task to complete before executing the task in the main queue, but the home row performs the task in the primary thread; The two of them are in the same thread now. The thread is executed sequentially, when the main queue routines and so on, and then go to continue execution , the host column will wait for the main thread to complete before returning to perform the task inside the home column.

7. serial queue is divided into two kinds

the Home row Dispatch_get_main_queue () host row is a special serial queue GCD comes with, and the tasks placed in the main queue are placed in the primary thread execution

Create a queue .

*/

@implementation Viewcontroller


-(void) Viewdidload {

[Super Viewdidload];

Get the home row by GCD

all queue receive queue objects are dispatch_queue_t

dispatch_queue_t mainqueue = Dispatch_get_main_queue ();

Perform tasks synchronously

Dispatch_sync (Mainqueue, ^{

NSLog (@ "Dispatch_sync");

//    });


Perform tasks asynchronously

///NSLog (@ " the main thread executes here, finds an asynchronous execution code that executes the code asynchronously, but does not wait for the task to execute before continuing with the main thread task ");

Dispatch_async (Mainqueue, ^{

NSLog (@ " Execute task asynchronously ");

//    });

dispatch_queue_t mainqueues = Dispatch_get_main_queue ();

//

Dispatch_async (Mainqueues, ^{

NSLog (@ "sssss");

//    });

//

the // serial queue has two ways of 1. Main queue 2. Create queue serial queues are executed in sequence

// How to create a queue yourself < serial queue >

dispatch_queue_t huluwaqueue = dispatch_queue_create (" Lake economy may be more objective ", NULL);

Dispatch_sync (Huluwaqueue, ^{

NSLog (@ "11111");

//    });

Dispatch_sync (Huluwaqueue, ^{

NSLog (@ "222222");

//    });

Dispatch_sync (Huluwaqueue, ^{

NSLog (@ "333333");

//    });

Dispatch_sync (Huluwaqueue, ^{

NSLog (@ "4444444");

//    });

// concurrent Queues can specify the priority of queue tasks to use global concurrent queues

#define Dispatch_queue_priority_high 2

#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0

#define Dispatch_queue_priority_low (-2)

#define Dispatch_queue_priority_background Int16_min

The //gcd default already provides a global concurrency queue for the entire app to use, without the need to manually create

Long identifier Specifies the priority level

dispatch_queue_t firstqueue = dispatch_get_global_queue (dispatch_queue_priority_high, 0);

dispatch_queue_t secondqueue = dispatch_get_global_queue (dispatch_queue_priority_low, 0);

dispatch_queue_t thirdqueue = dispatch_get_global_queue (dispatch_queue_priority_default, 0);

Dispatch_async (Firstqueue, ^{

NSLog (@ "11111");

});

Dispatch_async (Secondqueue, ^{

NSLog (@ "22222");

});

Dispatch_async (Thirdqueue, ^{

NSLog (@ "33333");

});

}




-(void) didreceivememorywarning {

[Super didreceivememorywarning];

Dispose of any resources the can be recreated.

}


@end


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The use of multi-threaded-GCD column

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.