[IOS interview series-2] multi-thread synchronization, asynchronous and serial, parallel logical relationship between (must be tested, must master), ios-2

Source: Internet
Author: User

[IOS interview series-2] multi-thread synchronization, asynchronous and serial, parallel logical relationship between (must be tested, must master), ios-2

API-https://developer.apple.com/library/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/index.html#//apple_ref/doc/uid/TP40008079

 

I. synchronous, asynchronous, serial, and parallel

Task serial execution means that only one task is executed at a time. Concurrent task execution means that multiple tasks can be executed at the same time.

A synchronous function is returned only after it has completed its scheduled task. An asynchronous function, on the contrary, will return immediately, and the scheduled task will be completed but will not wait for it to complete. Therefore, an asynchronous function does not block the current thread from executing the next function. (Source: http://www.cocoachina.com/industry/20140428/8248.html)

Queues are divided into serial and parallel

Task execution is divided into synchronous and asynchronous

------- The queue is only responsible for task scheduling, not task execution ---------

------- The task is executed in the thread ---------

Serial queue: The task is scheduled in order. The previous task is not executed and the queue is not scheduled.

Parallel queue: as long as there are Idle threads, the queue will schedule the current task and hand it over to the thread for execution. You do not need to consider that all the preceding tasks are being executed, as long as there are threads available for use, the queue schedules the task.

Synchronous execution: New threads are not enabled and tasks are executed in sequence.

Asynchronous execution: New threads are enabled and tasks can be executed concurrently.

(Source: http://www.cnblogs.com/KongPro/p/5193175.html)

[Note: there are contradictions between the two. One is that the queue executes tasks, and the other is that the queue does not execute tasks = the other is that synchronous Asynchronization is a problem returned, one problem is that synchronous Asynchronization cannot open threads] Sync)And Asynchronous (async)The main difference is whether the current thread will be blocked BlockThe task is completed!
If yes Sync)Operation, it will block the current thread and wait BlockAnd then the current thread will continue to run.
If yes Asynchronous (async)Operation, the current thread will be executed directly, it will not block the current thread. (Http://www.jianshu.com/p/0b0d9b1f1f19) [third person and the first person means almost]

Ii. Queue

GCD provides dispatch queues to process code blocks. These queue management tasks you provide to GCD are executed in FIFO order. This ensures that the first task to be added to the queue is the first task in the queue, and the second task to be added will start the second task, until the end of the queue.

1,Serial queue

The only thing that can be ensured is that GCD only executes one task at a time and runs it in the order we add it to the queue. Because no two tasks run concurrently in the serial queue.

For tasks placed in the serial queue, GCD willFIFO (FIFO)And then take the next one.

2,Concurrent queue

Tasks in the concurrent queue can be executed in the order they are added. The tasks may be completed in any order and you do not know when to start running the next task, or how many blocks are running at any time. Again, it depends entirely on GCD.

For tasks in the parallel queue, GCD will alsoFIFOBut the difference is that it will be put into another thread after it is retrieved and put into another thread. As a result, all the tasks are executed together. However, GCD controls the number of parallel tasks based on system resources. Therefore, if there are many tasks, it does not allow all tasks to be executed simultaneously.

(Source: http://www.cocoachina.com/industry/20140428/8248.html)

(Original link: http://www.jianshu.com/p/0b0d9b1f1f19)

 

3. Combination

(Source: http://www.cnblogs.com/KongPro/p/5193175.html)

[Personal conclusion: the difference between serial and parallel operations is whether a task will be placed in another thread.

The difference between synchronous and asynchronous operations is whether the current thread will be blocked

Serial synchronization: removes a task from other threads, blocks the current thread (does not open a new thread), finishes task execution, finishes task execution, and the next task

Serial Asynchronization: extracts a task and does not put it into another thread, does not block the current thread, and waits until the task is completed, the task is completed, and the next task is completed.

Parallel synchronization: a task can be retrieved and placed in another thread to block the current thread (no new thread is opened). After the task is executed, wait until the task is completed and the next task is executed.

Parallel Asynchronization: a task can be pulled out and placed in another thread. The current thread is not blocked, and the task is not completed, the task is completed, and the next task is completed.

]

[So the biggest problem now is what if only one thread executes tasks in a parallel queue asynchronously? In a sense, if the thread is not enabled, it is synchronous. Once Asynchronous is called, it indicates that the thread is enabled. Therefore, it is reasonable to sum up the three]

 

Let queue = dispatch_queue_create ("myQueue", DISPATCH_QUEUE_SERIAL)// Create a serial queueNSLog ("previous-% @", NSThread. currentThread () dispatch_async (queue, {()-> Void in// Asynchronous execution of the serial queue-A new thread NSLog ("before sync-% @", NSThread. currentThread () dispatch_sync (queue, {()-> Void in// Synchronous execution of the serial queue-Congestion the current thread and wait until the execution of the previous task is completed (and the previous task is blocked) NSLog ("sync-% @", NSThread. currentThread ()}) NSLog ("after sync-% @", NSThread. currentThread ()}) NSLog ("after-% @", NSThread. currentThread ())

[I am not at ease looking for a blog. I will update it again if I have time to read the relevant information in the book]

--------------------------------------------------------------------------------

Now I feel that the task is first put in the queue and synchronized or asynchronously executed from the queue. Of course, whether this is the case remains to be verified.

In this case, the serial queue can only pull out tasks one by one, whether you block the original thread or open new threads, the tasks are executed one by one.

For parallel queues, if it is to block the synchronous execution of the original thread, it can only be executed one by one. If it is not to block the original thread and then open the thread, it can be executed together.

Suspect:

1. is a task first taken out of the queue and executed according to the queue rules, especially serial Asynchronization (will new threads be opened ?) In terms of parallel synchronization, you must note how the communication between them is implemented?

A: In GDC, whether an operation is multi-threaded or single-threaded depends on the current queue type and execution method, only when the queue type is parallel queue and the asynchronous method is used for execution can it be executed in multiple threads.

2. Although the difference between synchronous and asynchronous is whether to block the original thread, is there a way to avoid blocking besides opening up new threads? Is there a thread?

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.