iOS signal Volume

Source: Internet
Author: User

Introduction:

When you take a local contact list, you see a colleague's code:

    dispatch_semaphore_t sema = dispatch_semaphore_create (0);         ^ (bool  Granted, cferrorref error) {            dispatch_semaphore_signal (SEMA);        });    Dispatch_semaphore_wait (Sema, dispatch_time_forever);

The semaphore is not used before, but the concept of semaphore is still there.

Signal Volume Overview (quote Baidu Encyclopedia):

Take the operation of a car park as an example. For simplicity, suppose that there are only three parking spaces in the car park and three spaces are empty at the beginning. At the same time, if five vehicles were to come, the janitor would allow three of them to enter directly, then put down the car block, the rest of the car must wait at the entrance, and then the car will have to wait at the entrance. At this time, there is a car left the parking lot, the janitor learned, open the car block, put into the outside of a car, if left two, then can put two, so reciprocating. In this parking system, parking spaces are public resources, and each car is like a thread, and the gatekeeper acts as the semaphore.

In abstract terms, the semaphore is characterized as follows: The semaphore is a nonnegative integer (number of spaces), and all threads/processes (vehicles) passing through it will subtract that integer by one (through it, of course, in order to use the resource), and when the integer value is zero, all threads attempting to pass through it will be in the waiting state. On the semaphore we define two actions: Wait (wait) and release (release). When a thread calls the wait operation, it either gets the resource and then cuts the semaphore by one, or waits until it is placed in the blocking queue until the semaphore is greater than or equal to a momentary amount. Release is actually an add operation on the semaphore, which corresponds to the vehicle leaving the parking lot, which is called "release" because of the release of resources guarded by semaphores.

Demo parsing

1)

    //     Create a semaphore with a value of 0            dispatch_semaphore_t sema = dispatch_semaphore_create (0);         // Signal is signaled     at the end of an operation, which causes the semaphore to be +1        Abaddressbookrequestaccesswithcompletion (addressbook, ^ (bool  Granted, cferrorref error) {            Dispatch_semaphore_signal (SEMA);        });     // At the     beginning of the execution here the semaphore is 0, the thread is blocked until the above operation completes so that the semaphore is +1, the thread is unblocked    dispatch_semaphore_wait (SEMA, dispatch_time_ FOREVER);

2)

    //Creating a groupdispatch_group_t Group =dispatch_group_create (); //creates a semaphore signal volume of tendispatch_semaphore_t semaphore = Dispatch_semaphore_create (Ten); //get the default global concurrency queuedispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0);  for(Inti =0; I < -; i++)    {        //since the semaphore is 10, there will be a maximum of 10 people in the queue and the task is executed,dispatch_semaphore_wait (Semaphore,dispatch_time_forever); //task added to group is monitoredDispatch_group_async (group, queue, ^{NSLog (@"%i", i); Sleep (2);        Dispatch_semaphore_signal (semaphore);    }); }    //wait for all tasks in the group to complete or blockdispatch_group_wait (Group, dispatch_time_forever);    Dispatch_release (group);    Dispatch_release (semaphore); 

@mic

(Email:[email protected])

(qq:839720759)

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.