IOS Signal Volume

Source: Internet
Author: User
Tags gcd

The semaphore is a resource counter that has two operations on the semaphore to achieve mutual exclusion, respectively, p and V operations. The general situation is such as critical access or mutually exclusive access: Set the semaphore value of 1, when a process 1 is running, using resources, the P operation, that is, the semaphore value minus 1, that is, the number of resources is less than 1. This is a semaphore value of 0. The system specifies that when the semaphore value is 0, you must wait and know that the semaphore value is not zero to continue operation. At this point if the process 2 wants to run, then also must do the p operation, but at this time the semaphore is 0, so cannot reduce 1, that is, cannot p operation, also blocked. This will go to process 1 exclusive access. When Process 1 finishes running, the resource is freed and the V operation is performed. The number of resources is re-added to 1, which is the semaphore value changed to 1. At this time, process 2 found that the number of resources is not 0, the semaphore can do p operation, immediately perform P operation. The semaphore value becomes 0. Number of times process 2 we have resources, exclusive access to resources. This is the principle of the semaphore to control mutual exclusion.

Definition:

1, Semaphore: is a type of identification that can be used to control the number of access resources, set a semaphore, before the thread access, plus the processing of semaphores, you can tell the system according to the amount of semaphore we specified to execute multiple threads.

In fact, this is a bit like the lock mechanism, but the semaphore is the system to help us to deal with, we just need to set a semaphore before executing the thread, and when used, plus the semaphore processing method is OK.

2, the signal volume has 3 functions, respectively:

// create semaphore, parameter: The initial value of the semaphore, or null if less than 0  // wait for lower semaphore  // increase semaphore dispatch_semaphore_signal (semaphore)

A simple introduction to these three functions, the first function has a shaping parameters, we can understand as the total amount of signal, dispatch_semaphore_signal is to send a signal, will naturally let the total amount of signal 1,dispatch_semaphore_ Wait for the signal, when the total signal amount of less than 0 will be waiting, otherwise it can be normal execution, and let the total signal-1, according to this principle, we can quickly create a concurrency control to synchronize tasks and limited resource access control. 3, first to a simple example
 //create a semaphore and set the value to tendispatch_semaphore_t semaphore = Dispatch_semaphore_create (Ten); dispatch_queue_t Queue= Dispatch_get_global_queue (Dispatch_queue_priority_default,0);  for(inti =0; I < -; i++)    {   //because it is executed asynchronously, every time the dispatch_semaphore_signal inside the block is not executed at all, it executes the dispatch_semaphore_wait. Thus semaphore-1. When Loop 10 thereafter, Semaphore equals 0, the thread will be blocked until dispatch_semaphore_signal execution of the block continues executiondispatch_semaphore_wait (semaphore, dispatch_time_forever); Dispatch_async (Queue,^{NSLog (@"Signal Volume-index=%i", i); Sleep (2); //each time the signal is sent, semaphore will be +1.dispatch_semaphore_signal (semaphore);    }); }

found that the task is a group of 10, each 2 seconds to execute a group. It can be understood that the maximum number of concurrent numbers is 10.

3, is not a little feeling, then another example, it is more clear

-(void) dispatchsignal{//The value of crate indicates that up to a few resources can be accesseddispatch_semaphore_t semaphore = Dispatch_semaphore_create (2); dispatch_queue_t Quene= Dispatch_get_global_queue (Dispatch_queue_priority_default,0); //Task 1Dispatch_async (Quene, ^{dispatch_semaphore_wait (semaphore, dispatch_time_forever); NSLog (@"Run Task 1"); Sleep (1); NSLog (@"Complete Task 1");            Dispatch_semaphore_signal (semaphore); });<br>//Task 2Dispatch_async (Quene, ^{dispatch_semaphore_wait (semaphore, dispatch_time_forever); NSLog (@"Run Task 2"); Sleep (1); NSLog (@"Complete Task 2");            Dispatch_semaphore_signal (semaphore); });<br>//Task 3Dispatch_async (Quene, ^{dispatch_semaphore_wait (semaphore, dispatch_time_forever); NSLog (@"Run Task 3"); Sleep (1); NSLog (@"Complete Task 3");            Dispatch_semaphore_signal (semaphore);    }); }

Execution Result:

  

Summary: Since the set signal value is 2, the execution of two threads, and so on after the execution of one, will continue to execute the next, to ensure that the number of threads executed at the same time not more than 2.

Here we expand, assuming we set the signal value =1

dispatch_semaphore_t semaphore = dispatch_semaphore_create (1);

So the result is:

If you set the signal value =3

dispatch_semaphore_t semaphore = dispatch_semaphore_create (3);

So the result is:

In fact, set to 3, is not to limit the execution of threads, because there are only 3 threads.

Resources:

talking about the signal quantity in GCDIOS GCD Intermediate-dispatch_semaphore (semaphore) understanding and use

IOS Signal Volume

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.