GCD details and macd indicators details

Source: Internet
Author: User

GCD details and macd indicators details
I. Advantages of GCD
1. GCD is a solution developed by Apple for multi-core parallel computing. 2. GCD will automatically use more CPU cores (such as dual-core and quad-core ). 3. GCD automatically manages the thread lifecycle (creating threads, scheduling tasks, and destroying threads ).
2. GCD has two core concepts: 1. Task 2. Queue
3. Use CGD Step 1. Customize the Task 2. Add the task to the queue, and CGD will automatically remove the task in the queue and put it in the corresponding thread for execution. The task extraction follows the forward d Principle of the queue, namely, first-in-first-out and later-out.
Iv. Differences between synchronization and Asynchronization 1. synchronization Can only execute tasks in the current thread, but cannot start new threads. asynchronization can execute tasks in new threads and enable new threads.
5. CGD queues can be divided into two categories: 1. Concurrent queues (1). Concurrent execution of multiple tasks (automatic enabling of multiple threads to execute tasks at the same time ). (2). the concurrent function is only valid under the asynchronous (dispatch_async) function.
2. The serial queue allows tasks to be executed one by one (after a task is executed, the next task is executed ).
6. synchronization, Asynchronization, concurrency, and serial relationship 1. Synchronization and Asynchronization mainly affect whether new threads can be enabled. 2. Synchronization: The task is executed in the current thread and cannot start a new thread. 3. asynchronous: execute tasks in new threads and be able to start new threads. 4. Concurrency and serial mainly affect execution in any way. 5. Concurrency: Concurrent execution of multiple tasks. 6. Serial. After a task is executed, the next task is executed.


7. There are two functions in GCD to execute the task. 1. Execute the task in synchronous mode.
Dispatch_sync (dispatch_queue_t queue, dispatch_block_t block );
The queue is a queue and the block is a task.
2. Execute the task in asynchronous mode
Dispatch_async (dispatch_queue_t queue, dispatch_block_t block );

8. By default, GCD provides a global concurrency queue for the entire application. You do not need to create it manually.
Use the dispatch_get_global_queue function to obtain the global concurrent queue.
Dispatch_queue_t dispatch_get_global_queue (
Dispatch_queue_priority_t priority, // queue priority
Unsigned long flags); // this parameter is temporarily useless. Use 0.
Dispatch_queue_t queue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // obtain the global concurrency queue

9. Priority of global concurrent queues
# Define DISPATCH_QUEUE_PRIORITY_HIGH 2 // high
# Define DISPATCH_QUEUE_PRIORITY_DEFAULT 0 // default (medium)
# Define DISPATCH_QUEUE_PRIORITY_LOW (-2) // low
# Define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN // background

10. There are two methods to obtain serial numbers in GCD.
1. Use the dispatch_queue_create function to create a serial queue
Dispatch_queue_t
Dispatch_queue_create (const char * label, // queue name
Dispatch_queue_attr_t attr); // queue attribute, which can be NULL.
Dispatch_queue_t queue = dispatch_queue_create ("cn. itcast. queue", NULL); // create Note: manually create a serial queue. Non-ARC must release the queue manually created
Dispatch_release (queue );

2. Use the main queue column (queue associated with the main thread)
The main queue is a special serial queue that comes with GCD.
All tasks in the main queue are executed in the main thread.
Use dispatch_get_main_queue () to obtain the main queue Column
Dispatch_queue_t queue = dispatch_get_main_queue ();

Note: If you use the sync function to add tasks to the main queue, the current serial queue will be stuck.

11. All the words with the create, copy, new, and retain function names should be release when this data is not needed.
The data type of GCD does not need to be release in the ARC environment.
The data type of CF (Core Foundation) still needs to be release in the ARC environment.

Note: NSString * str1 = @ "NSString"; // Founation
CFStringRef str2 = (_ bridge CFStringRef) (str1); // Core Foundation
_ Bridge CFStringRef indicates a bridge. The OC string is automatically converted into a Core Foundation string.

CFArrayRef array = CFArrayCreate (NULL, NULL, 10, NULL );
CFRelease (array );
The data type manually created in Core Foundation. You need to manually release the memory. CFRelease (); releases all memory types manually created by the Core Foundation framework.

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.