[Original] GCD Programming

Source: Internet
Author: User

In the previous article, I talked about the basic usage of GCD. In this article, I will subscribe a little more about the 'synchronization' in GCD programming.

Let's first look at the prototype:

-() asyncMethodForPrint:(NSString *-(*arr = [NSArray arrayWithObjects:,,,,,, (NSString *item 

Obviously, the above is based on ~ G is used for log output. What is a form of cpu utilization for a multi-core mobile device? Of course, it is done with GCD. Through GCD, the system usually maintains a work thread pool by itself. When a task needs to be executed, the above code, in The GCD programming mode, can be used directly from the thread pool in the form of concurrent execution:

-() asyncMethodForPrint:(NSString *-(*arr = [NSArray arrayWithObjects:,,,,,, (NSString *item , ), ^

It's very simple. Now, we repeat the content mentioned in the previous article. Of course, the log will not be output in order,:

-- :: GCDExample[:-- :: GCDExample[-- :: GCDExample[:-- :: GCDExample[-- :: GCDExample[-- :: GCDExample[-- :: GCDExample[:] asyncMethodForPrint[g]

About wait

 

Now, the problem is that our current task is only print independent and unrelated strings. Of course, we can use the concurrent queue to take the form of concurrency, but if the business changes, we need to print the number of times the asyncMethodForPrint method is executed after the for Loop:

  counter = -() asyncMethodForPrint:(NSString *++-(*arr = [NSArray arrayWithObjects:,,,,,, (NSString *item , ), ^

The result of count printing is either 0 or 1, because dispatch_async returns immediately. To solve this problem, the idea is to wait until all the previous tasks are finished before the count is output. So we need to introduce the concept of group,

The modified code is as follows:

Counter =-() asyncMethodForPrint :( NSString * // This is mainly used to introduce the group. It is more rigorous and needs to be introduced.Dispatch_semaphore_signalNSLog (++-(* arr = [NSArray arrayWithObjects:, dispatch_group_t group_1 = (NSString * item dispatch_group_async (group_1, substring (,), ^

The above code, if you know about distributed computing, does it feel similar to 'map reduce? Haha.

Then, we can see that the introduction of dispatch_group_t can bring us some 'synchronization' implementations, and related APIS also include dispatch_group_wait. Please refer to the Lib Doc on your own.

GCD also has some APIs, such as semaphore semaphores, dispatch source, and asynchronous io. There are not many opportunities to use them at ordinary times, so I will not study them in depth for the moment, maybe there will be a GCD-3 in the future, which means it is actually used.

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.