GCD use of Multi-threading (ii)

Source: Internet
Author: User

Record the usage of several important APIs for GCD.

Dispatch_queue_create

*stringis** * * * * * The newly created Dispatch queue.dispatch_queue_tdispatch_queue_create (constchar *label, Dispatch_queue_ attr_t attr);

Used to create the dispatch Queue, the return value type is dispatch_queue_t, this method needs to pass two parameters, label is a string tag attached to the queue, this parameter is optional, and may be empty, this is the name of the queue, It is convenient to debug, so it is recommended to pass a label when creating the dispatch queue. Here's how to get the name of a queue:

/** **constChar *Dispatch_queue_get_label (dispatch_queue_t queue); 

The second parameter, attr, is used to specify the type of DISPATCH QUEUE created, dispatch_queue_serial or null to create a serial DISPATCH Queue,dispatch_queue_ Concurrent represents the creation of a parallel dispatch Queue. In fact, the creation of serial parameters dispatch_queue_serial is null, the source code is as follows:

/**/#define dispatch_queue_serial NULL

FIFO, that is, first out, FIFO.

With regard to the two Dispatch queue types created, Serial Dispatch queue can only perform one append processing at a time, Concurrent Dispatch queue is capable of appending multiple processing in parallel, although both Dispatch The queue is constrained by system resources, but you can use the Dispatch_queue_create function to generate any number of dispatch queues. When multiple serial Dispatch queues are generated, each serial Dispatch queue is executed in parallel, although only one append processing can be performed in a serial Dispatch queue, if the processing is appended to 4 serial respectively Dispatch queue, each serial Dispatch queue executes one, that is, 4 processing at a time. That is, once the serial Dispatch queue is generated and appended, the system will generate a serial Dispatch queue and generate only one thread, and if multiple serial Dispatch queues are generated, then the system generates multiple threads.

Although multiple threads can be generated, the serial Dispatch Queue must never be abused, because if too many threads are used, it consumes a lot of memory, causing a lot of context switching and greatly reducing the response performance of the system. Therefore, it is recommended to use the serial Dispatch Queue only in order to prevent multiple threads from updating the same resource to cause data contention.

When you want to perform parallel processing without data contention, use the concurrent Dispatch queue, for concurrent Dispatch queue, no matter how much is generated, because the XNU kernel only uses effectively managed threads, So there is no problem with serial Dispatch queue.

In addition, the dispatch queue generated by the Diaptch_queue_create function needs to be freed through the Dispatch_release function after the end of use.

Dispatch_release (queue);

Correspondingly, the GCD also has retain operations, and the Dispatch_retain function is to increase the reference count. The dispatch queue in GCD, like Objectiv-c's reference-technology memory management, needs to manage memory through the reference count of Dispatch_retain and Dispatch_release.

Finally recommend a very good blog: Tang Qi's Technical blog.

GCD use of Multi-threading (ii)

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.