GCD preliminary learning and GCD Learning

Source: Internet
Author: User
Tags lock queue

GCD preliminary learning and GCD Learning

GCD should be awesome. It may be difficult to operate at half past one.

There are two articles on GCD in cocoa-china. GCD is an in-depth understanding (1) GCD (2)

CSDN Rong Fangzhi blog: Click to open the link

I just read the first article. Here I will record several operation functions:

GCD is a system used to optimize programs that support multi-core processors and multi-processing systems. It is built on the thread pool mode.

The principle is: Put tasks (functions or blocks) into a queue. There are two types of queues (first-in-first-out): parallel and serial;

Serial queue-the tasks in the queue are serialized. After one task is run, the next task is run. However, the queue and the queue are parallel.

Parallel queue-the tasks in the queue are parallel, so the order of termination is unknown;


The system provides four global concurrent Queues with different priorities: background, low, default, and high.

#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 system also provides you with a special queue called the main queue column, which is a serial queue, mainly used with UI updates;


Common Methods:


We can create a queue ourselves.

<span style="font-family:SimSun;font-size:12px;">dispatch_queue_t queue=dispatch_queue_create("queue1", DISPATCH_QUEUE_SERIAL);</span>

The first parameter is the queue name. The second parameter has two types: DISPATCH_QUEUE_SERIAL, DISPATCH_QUEUE_CONCURRENT. The first parameter is to create a serial queue, and the second parameter is to create a parallel queue.


Dispatch_async

To prevent the interface from being stuck during processing, we can use dispatch_async to execute network requests and other operations in another thread, and the main thread will continue to execute the UI operations;

Dispatch_async (dispatch_get_global_queue (queue, 0), ^ {// perform time-consuming operations dispatch_async (dispatch_get_main_queue (), ^ {// update interface });});


dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
It is used to obtain a global queue. The first parameter is one of the above four global queues. The second parameter does not know what it means. By default, it is set to 0.

The preceding operations in the block are put into the obtained global queue for execution. After the execution is complete, the system obtains the main queue column to update the ui;

Dispatch_group_async

You can monitor whether a group of tasks are completed, and send a notification after completion.

Dispatch_queue_t queue = queue (queue, 0); // obtain a global concurrent queue dispatch_group_t group = dispatch_group_create (); // create a Task group dispatch_group_async (group, queue, ^ {[NSThread sleepForTimeInterval: 1]; // NSLog (@ "group1 ");});
// Add a task to the group
Dispatch_group_async (group, queue, ^ {[NSThread sleepForTimeInterval: 2]; // NSLog (@ "group2 ");}); <pre name = "code" class = "objc" style = "color: rgb (120, 73, 42); font-size: 11px; "> // Add a task to the group
Dispatch_group_async (group, queue, ^ {[NSThread sleepForTimeInterval: 3]; // NSLog (@ "group3 ");});
// Add a task to the group
Dispatch_group_notify (group, dispatch_get_main_queue (), ^ {NSLog (@ "updateUi") ;}); // send a notification to dispatch_release (group) When all tasks in the group are completed;

 

Dispatch_barrier_async

The task is executed only after the previous task is finished in a queue, and the serial sequence is maintained when the task is executed (that is, the execution status of the original queue is restored after all the content of the task is executed );

dispatch_queue_t queue = dispatch_queue_create("gcdtest.rongfzh.yc", DISPATCH_QUEUE_CONCURRENT);  dispatch_async(queue, ^{      [NSThread sleepForTimeInterval:2];      NSLog(@"dispatch_async1");  });  dispatch_async(queue, ^{      [NSThread sleepForTimeInterval:4];      NSLog(@"dispatch_async2");  });  
// The following is the lock queue (queue, ^ {NSLog (@ "queue"); [NSThread sleepForTimeInterval: 4] ;}); dispatch_async (queue, ^ {[NSThread sleepForTimeInterval: 1]; NSLog (@ "dispatch_async3 ");});

















What does gcd (0, 0) mean in mathematics?

It seems to be gcd (int a, int B). This is a programming language that calculates the maximum common approx. And the minimum common I found online. The program is as follows: # include <stdio. h>

Int gcd (int a, int B) // calculates the maximum approximate number.
{
If (a <B)
Return gcd (B, );
If (a % B = 0)
Return B;
Return gcd (B, a % B );
}

Int main ()
{
Int a, B;

Scanf ("% d", & a, & B );

Printf ("maximum common divisor: % d \ n", gcd (a, B ));
Printf ("minimum public multiple: % d \ n", a * B/gcd (a, B ));

Return 0;
}

Mathematical operator number gcd

Gcd indicates the maximum common divisor, as shown in figure
Gcd {2, 6} = 2
Gcd {2, 3} = 1

Related Article

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.