Concurrent programming GCD rough memory method

Source: Internet
Author: User

These two days of the thread, the apple of this set of C language framework GCD very interested, simply to go down, found that many of the concept is not clear, because I this fish-like memory, to remember

1. The concept of first contact must be process and thread, what? Processes and threads also need to remember, then do not engage in it, but also to explain that the process is like a running program, the thread is the execution of the code inside the program, if the program has multiple lines of code execution, then it involves multithreading.

2. Operation queue, it is easy to misunderstand that the queue is a thread, but it is not, this thing is just a queue (FIFO), the queue is a task, that is, code.

3. The home row, the home row is nothing magical, it is also the operation queue, but it is created by the system queue, is similar to the serial queue, high priority

Global queue, Dispatch_get_global_queue (Dispatch_queue_priority_default, 0), similar to a parallel queue, but unable to determine which queue.

4. Synchronization tasks, synchronization tasks have a high priority, and in the same queue have a sequence of execution, must wait until the synchronization task is finished to perform subsequent tasks, it is possible to block the thread.

5. Asynchronous tasks, the priority of the asynchronous task is low, and so the system is more idle when there is nothing to do when it executes, in the queue does not have the order of execution, does not block the thread.

6. Serial queue, this serial queue, is essentially a queue, plus the word "serial", it means that the serial queue to be like in the circuit parallel series, the task will be executed by one, whether your task is asynchronous or synchronous.

7. Parallel queue, the essence is also the queue, plus "parallel" two words, the parallel queue like a parallel circuit, the task can be (at the same time) execution, here at the same time is not the real sense at the same time, the specific explanation in the operating system CPU work on the way there is elaborated, we just care about it similar "at the same At the same time "the effect of execution is OK."

So here's the problem, the combination of the queue and the task:

1. Serial + Sync

The tasks in the serial queue are executed one after the other, and the synchronization tasks are executed in the order of execution, so the conclusion is that the synchronization tasks in the serial queue will be executed one after the other.

dispatch_queue_t q1 = dispatch_queue_create ("Serial queue", dispatch_queue_serial);

Dispatch_sync (Q1, ^{

[Nsthread Sleepfortimeinterval:5];

NSLog (@ "Serial queue synchronization Task 1%@", [Nsthread CurrentThread]);

});

Dispatch_sync (Q1, ^{

NSLog (@ "Serial Queue synchronization Task 2%@", [Nsthread CurrentThread]);

});

Dispatch_sync (Q1, ^{

NSLog (@ "Serial Queue synchronization Task 3%@", [Nsthread CurrentThread]);

});

Printing results:

2014-12-09 17:43:55.991 Multithreading test[4696:942016] Serial Queue synchronization task 1 <nsthread:0x7fac3bc24860>{number = 1, name = main}

2014-12-09 17:43:55.992 Multithreading test[4696:942016] Serial Queue synchronization task 2 <nsthread:0x7fac3bc24860>{number = 1, name = main}

2014-12-09 17:43:55.992 Multithreading test[4696:942016] Serial Queue synchronization task 3 <nsthread:0x7fac3bc24860>{number = 1, name = main}

It is easy to see the problem, the synchronization task in the serial queue is automatically added to the main thread, and the main thread is blocked, and the UI is not updated until the task is completed

2. Serial + Asynchronous

The Serial queue task is executed one after the other, the asynchronous task is not sequential, the two are grouped together to follow the rules of the queue, that is, whether you are synchronous or asynchronous, you need to obey the rules of the queue, so the task is to be executed one by one sequentially.

dispatch_queue_t q1 = dispatch_queue_create ("Serial queue", dispatch_queue_serial);

Dispatch_async (Q1, ^{

[Nsthread Sleepfortimeinterval:5];

NSLog (@ "Serial queue asynchronous Task 1%@", [Nsthread CurrentThread]);

});

Dispatch_async (Q1, ^{

NSLog (@ "Serial queue asynchronous Task 2%@", [Nsthread CurrentThread]);

});

Dispatch_async (Q1, ^{

NSLog (@ "Serial queue asynchronous Task 3%@", [Nsthread CurrentThread]);

});

Printing results:

2014-12-09 17:49:54.906 Multithreading test[4834:957312] Serial Queue Asynchronous task 1 <nsthread:0x7fa35bd13750>{number = 2, name = (null)}

2014-12-09 17:49:54.907 Multithreading test[4834:957312] Serial Queue Asynchronous task 2 <nsthread:0x7fa35bd13750>{number = 2, name = (null)}

2014-12-09 17:49:54.907 Multithreading test[4834:957312] Serial Queue Asynchronous task 3 <nsthread:0x7fa35bd13750>{number = 2, name = (null)}

It is easy to see that the asynchronous task of the serial queue is actually executed sequentially, but it does not go into the main thread, does not block the main thread, updates the UI first, and then displays the result.

3. Serial + synchronous + asynchronous

dispatch_queue_t q1 = dispatch_queue_create ("Serial queue", dispatch_queue_serial);

Dispatch_async (Q1, ^{

NSLog (@ "Serial queue asynchronous Task 1%@", [Nsthread CurrentThread]);

});

Dispatch_sync (Q1, ^{

[Nsthread Sleepfortimeinterval:5];

NSLog (@ "Serial Queue synchronization Task 2%@", [Nsthread CurrentThread]);

});

Dispatch_async (Q1, ^{

NSLog (@ "Serial queue asynchronous Task 3%@", [Nsthread CurrentThread]);

});

Printing results:

2014-12-09 17:56:56.339 Multithreading test[5117:972472] Serial Queue Asynchronous task 1 <nsthread:0x7fc4217564f0>{number = 2, name = (null)}

2014-12-09 17:57:01.342 Multithreading test[5117:972174] Serial Queue synchronization task 2 <nsthread:0x7fc421416db0>{number = 1, name = main}

2014-12-09 17:57:01.342 Multithreading test[5117:972472] Serial Queue Asynchronous task 3 <nsthread:0x7fc4217564f0>{number = 2, name = (null)}

It is easy to see whether there is asynchronous synchronization or compliance with the serial queue's task order, but the main thread is still added to the synchronization task, and the main thread is blocked, affecting the UI display.

4. Serial + synchronous (nested async)

dispatch_queue_t q1 = dispatch_queue_create ("Serial queue", dispatch_queue_serial);

Dispatch_sync (Q1, ^{

NSLog (@ "Serial queue synchronization Task 1%@", [Nsthread CurrentThread]);

[Nsthread Sleepfortimeinterval:5];

Dispatch_async (Q1, ^{

NSLog (@ "Serial queue asynchronous Task 2%@", [Nsthread CurrentThread]);

});

});

Printing results:

2014-12-09 18:01:12.259 Multithreading test[5276:982408] Serial Queue synchronization task 1 <nsthread:0x7fa56a516280>{number = 1, name = main}

2014-12-09 18:01:17.262 Multithreading test[5276:982631] Serial Queue Asynchronous task 2 <nsthread:0x7fa56a712aa0>{number = 2, name = (null)}

It is easy to see the problem, the task is still executed sequentially, normal.

5. Serial + asynchronous (nested synchronization)

dispatch_queue_t q1 = dispatch_queue_create ("Serial queue", dispatch_queue_serial);

Dispatch_async (Q1, ^{

NSLog (@ "Serial queue asynchronous Task 1%@", [Nsthread CurrentThread]);

Dispatch_sync (Q1, ^{

NSLog (@ "Serial Queue synchronization Task 2%@", [Nsthread CurrentThread]);

});

});

Printing results:

2014-12-09 18:04:03.168 Multithreading test[5389:990941] Serial Queue Asynchronous task 1 <nsthread:0x7fa399599d40>{number = 2, name = (null)}

The problem came, Task 2 did not execute, the result is not I paste less, the program does carry out here to block no longer execute, let's analyze why

Serial queue needs to be executed sequentially, then task 1 to execute first, you can perform task 2, but task 1 has a synchronous task 2, the synchronization task will not open new threads, will automatically join the main thread, so that task 2 will never be executed, Task 2 does not perform task 1 will never end, Task 1 does not end, Task 2 will not execute ..... (Open Tang Sanzang mode)

6. Home row + Sync

dispatch_queue_t q = Dispatch_get_main_queue ();

Dispatch_sync (q, ^{

NSLog (@ "Home row sync Task 1%@", [Nsthread CurrentThread]);

});

No print results

The main queue is a running loop, if join synchronization task, will cause the host column block, the program will be locked, (re-open the Tang Sanzang mode), run loop does not end, do not perform synchronization tasks, synchronization task does not execute, run loop does not end ....

7. Home Row + Async

dispatch_queue_t q = Dispatch_get_main_queue ();

Dispatch_async (q, ^{

NSLog (@ "Home row Async Task 1%@", [Nsthread CurrentThread]);

});

Dispatch_async (q, ^{

NSLog (@ "Home row Async Task 2%@", [Nsthread CurrentThread]);

});

Dispatch_async (q, ^{

NSLog (@ "Home row Async Task 3%@", [Nsthread CurrentThread]);

});

Printing results:

2014-12-10 13:34:13.427 Multithreading test[1570:369316] Home column Asynchronous task 1 <nsthread:0x7fc9a2611fc0>{number = 1, name = main}

2014-12-10 13:34:13.427 Multithreading test[1570:369316] Home column Asynchronous task 2 <nsthread:0x7fc9a2611fc0>{number = 1, name = main}

2014-12-10 13:34:13.428 Multithreading test[1570:369316] Home column Asynchronous task 3 <nsthread:0x7fc9a2611fc0>{number = 1, name = main}

The asynchronous task of the home column is equivalent to the synchronous task, and asynchronous and synchronous are for the main thread, so the tasks that run on the main thread are definitely sequential.

8. Parallel + Sync

dispatch_queue_t q = dispatch_queue_create ("Parallel queue", dispatch_queue_concurrent);

Dispatch_sync (q, ^{

NSLog (@ "Parallel queue synchronization Task 1%@", [Nsthread CurrentThread]);

});

Dispatch_sync (q, ^{

NSLog (@ "Parallel queue synchronization Task 2%@", [Nsthread CurrentThread]);

});

Dispatch_sync (q, ^{

NSLog (@ "Parallel queue synchronization Task 3%@", [Nsthread CurrentThread]);

});

Printing results:

2014-12-10 13:38:51.408 Multithreading test[1709:378774] Parallel Queue Synchronization task 1 <nsthread:0x7fb7ca516520>{number = 1, name = main}

2014-12-10 13:38:51.409 Multithreading test[1709:378774] Parallel Queue Synchronization task 2 <nsthread:0x7fb7ca516520>{number = 1, name = main}

2014-12-10 13:38:51.409 Multithreading test[1709:378774] Parallel Queue Synchronization task 3 <nsthread:0x7fb7ca516520>{number = 1, name = main}

Executed sequentially.

9. Parallel + Asynchronous

dispatch_queue_t q = dispatch_queue_create ("Parallel queue", dispatch_queue_concurrent);

Dispatch_async (q, ^{

NSLog (@ "Parallel queue asynchronous Task 1%@", [Nsthread CurrentThread]);

});

Dispatch_async (q, ^{

NSLog (@ "Parallel queue asynchronous Task 2%@", [Nsthread CurrentThread]);

});

Dispatch_async (q, ^{

NSLog (@ "Parallel queue asynchronous Task 3%@", [Nsthread CurrentThread]);

});

Printing results:

2014-12-10 13:40:52.680 Multithreading test[1754:384474] Parallel Queue Asynchronous task 1 <nsthread:0x7fe9087034f0>{number = 4, name = (null)}

2014-12-10 13:40:52.680 Multithreading test[1754:384476] Parallel Queue Asynchronous task 3 <nsthread:0x7fe90863c660>{number = 3, name = (null)}

2014-12-10 13:40:52.680 Multithreading test[1754:384475] Parallel Queue Asynchronous task 2 <nsthread:0x7fe9084792a0>{number = 2, name = (null)}

Open multiple threads with no order of tasks and concurrent execution

10. Parallel + synchronous (nested async)

dispatch_queue_t q = dispatch_queue_create ("Parallel queue", dispatch_queue_concurrent);

Dispatch_sync (q, ^{

NSLog (@ "Parallel queue synchronization Task 1%@", [Nsthread CurrentThread]);

Dispatch_async (q, ^{

NSLog (@ "Parallel queue asynchronous Task 2%@", [Nsthread CurrentThread]);

});

});

Printing results:

2014-12-10 13:43:34.638 Multithreading test[1831:391846] Parallel Queue Synchronization task 1 <nsthread:0x7fddd3d281f0>{number = 1, name = main}

2014-12-10 13:43:34.639 Multithreading test[1831:391864] Parallel Queue Asynchronous task 2 <nsthread:0x7fddd3c0a4e0>{number = 2, name = (null)}

Concurrent execution.

11. Concurrency + async (nested synchronization)

dispatch_queue_t q = dispatch_queue_create ("Parallel queue", dispatch_queue_concurrent);

Dispatch_async (q, ^{

NSLog (@ "Parallel queue asynchronous Task 1%@", [Nsthread CurrentThread]);

Dispatch_sync (q, ^{

NSLog (@ "Parallel queue synchronization Task 2%@", [Nsthread CurrentThread]);

});

Dispatch_sync (q, ^{

NSLog (@ "Parallel queue synchronization Task 3%@", [Nsthread CurrentThread]);

});

Dispatch_sync (q, ^{

NSLog (@ "Parallel queue synchronization Task 4%@", [Nsthread CurrentThread]);

});

Dispatch_sync (q, ^{

NSLog (@ "Parallel queue synchronization Task 5%@", [Nsthread CurrentThread]);

});

Dispatch_sync (q, ^{

NSLog (@ "Parallel queue synchronization Task 6%@", [Nsthread CurrentThread]);

});

});

Printing results:

2014-12-10 13:51:42.396 Multithreading test[2409:421421] Parallel Queue Asynchronous task 1 <nsthread:0x7f9cebf0dab0>{number = 2, name = (null)}

2014-12-10 13:51:42.396 Multithreading test[2409:421421] Parallel Queue Synchronization task 2 <nsthread:0x7f9cebf0dab0>{number = 2, name = (null)}

2014-12-10 13:51:42.396 Multithreading test[2409:421421] Parallel Queue Synchronization task 3 <nsthread:0x7f9cebf0dab0>{number = 2, name = (null)}

2014-12-10 13:51:42.396 Multithreading test[2409:421421] Parallel Queue Synchronization task 4 <nsthread:0x7f9cebf0dab0>{number = 2, name = (null)}

2014-12-10 13:51:42.397 Multithreading test[2409:421421] Parallel Queue Synchronization task 5 <nsthread:0x7f9cebf0dab0>{number = 2, name = (null)}

2014-12-10 13:51:42.397 Multithreading test[2409:421421] Parallel Queue Synchronization task 6 <nsthread:0x7f9cebf0dab0>{number = 2, name = (null)}

Parallel queue + a asynchronous task (nested B-sync) at this point the B-Sync task synchronizes to the same thread as the parent task, the a task. (This study proves that all the tasks in the main queue will be added to the main thread, the asynchronous task is executed when the CPU is idle, the task will not open new threads by default, only the serial queue will open 1 threads asynchronously, parallel and asynchronous will open n threads, global plus asynchronously will add n threads)

Global and concurrency ...

Summary of the violent memory method, which is mainly to understand the concept of good, queue, thread, synchronous, asynchronous.

Violence Memory Method: The main queue task is in the main thread, the nature of the same serial.

Asynchronous tasks such as CPU idle execution.

The task does not open a thread, serial and asynchronous threads, parallel and asynchronously open n threads, global and parallel.

Serial plus any task nested synchronization tasks are deadlocked, other queues are not executed, and the home column is blocking the program.

Excerpt some of the explanations of netizens, feel very in place:

iOS Three multithreading technology:

1.NSThread

(1) It is very convenient to build a thread using the Nsthread object

(2) but! It is very difficult to manage multiple threads using Nsthread, it is not recommended to use

(3) tips! Use [Nsthread CurrentThread] to track task threads for these three technologies

2.nsoperation/nsoperationqueue

(1) is a set of Objective-c APIs implemented using GCD

(2) is an object-oriented threading technology

(3) provides some features that are not easy to implement in GCD, such as limiting the maximum number of concurrent and the dependencies between operations

3.gcd--grand Central Dispatch

(1) is a C-based underlying API

(2) define the task with block, which is very flexible and convenient to use.

(3) provides additional control capabilities and underlying functions that are not available in the Operation queue

1----The difference between a queue and a thread:

Queue: is the management thread, equivalent to the thread pool , can manage when the thread executes.

Queues are divided into serial queues and parallel queues, etc.

Serial queue: Threads in the queue are executed sequentially (not concurrently)

Parallel queues: Threads in a queue are executed concurrently, and there may be a question that the queue is not FIFO, and if the later tasks are finished, how to get out. It needs to be stressed here that the task is done, not necessarily out of the queue. The queue will only be out if the previous task is done.

2----- The queue created by the main thread queue and GCD is also different.

The main thread queue and the queue created by GCD are different. The queue priority created in GCD does not have a home column height, so there is no nested task inside the serial queue in GCD that does not block the main thread, only one of which could lead to deadlocks, which is the serial queue, nested open tasks that could lead to deadlocks.

Synchronization cannot be turned on in the main thread queue, blocking the main thread. Only the asynchronous task can be turned on, and the asynchronous task will not open the new thread, just reduce the priority of the asynchronous task, let the CPU idle time to call. synchronous tasks, which preempt the main thread's resources, can cause deadlocks.

3-----Threads: There are a lot of tasks (synchronous, asynchronous)

The difference between synchronous and asynchronous:

The synchronization task has a high priority, there is a sequence of executions in the thread, and no new threads are opened.

Asynchronous task priority is low, execution in thread is not sequential, see CPU idle. New threads are not opened in the primary queue, and other queues open new threads.

4----Main thread queue Note:

The following code execution order

1111

2222

Home Column Async <nsthread:0x8e12690>{name = (null), num = 1}

In the main queue, the asynchronous task is turned on, but the code in the code block is still executed in the main thread instead of opening new threads. Why isn't the thread blocked?

> The Home column opens the asynchronous task, although it does not open a new thread, but it lowers the priority of the asynchronous task, and at the same time, executes the asynchronous task on the main thread.

Why do I block threads when the synchronization task is turned on in the main queue?

> The synchronization task is turned on in the main queue because the host column is a serial queue, the threads inside are sequential, the next thread executes after executing a thread, and the home column always has only one main thread, and the main thread is not executed because he is an infinite loop, unless the application is closed. Therefore, in the main thread to open a synchronization task, the synchronization task will want to preempt the execution of resources, and the main thread task has been performing certain operations, refused to let go. Two priority levels are high, resulting in deadlocks and blocking threads.

Concurrent programming GCD rough memory method

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.