About the use of dispatch_semaphore

dispatch_semaphore is a method used by GCD for synchronization. There are three functions related to it, namely

dispatch_semaphore_create,dispatch_semaphore_signal,dispatch_semaphore_wait。

Below we introduce the three functions one by one:

(1) The declaration of dispatch_semaphore_create is:

  dispatch_semaphore_t  dispatch_semaphore_create(long value);

  The incoming parameter is long, and a semaphore of type dispatch_semaphore_t and value is output.

  It is worth noting that the incoming parameter value must be greater than or equal to 0, otherwise dispatch_semaphore_create will return NULL.

  (About the semaphore, I will not repeat it here, there are many introductions on the Internet. Here we mainly talk about the usage of the three functions dispatch_semaphore).

 

(2) The declaration of dispatch_semaphore_signal is:

  long dispatch_semaphore_signal(dispatch_semaphore_t dsema)

  This function will increase the value of the incoming semaphore dsema by 1; (as for the return value, we will talk about it later)

 

 (3) The declaration of dispatch_semaphore_wait is:

  long dispatch_semaphore_wait(dispatch_semaphore_t dsema, dispatch_time_t timeout);

  This function will decrement the value of the incoming semaphore dsema by 1;

  The function of this function is like this, if the value of the dsema semaphore is greater than 0, the thread where the function is located continues to execute the following statement and decrements the value of the semaphore by 1;

  If the value of desema is 0, then this function blocks the current thread to wait for the timeout (note that the type of timeout is dispatch_time_t,

  You cannot directly pass in an integer or float type), if the value of desema is incremented by the dispatch_semaphore_signal function during the waiting period,

  And the thread where the function (ie dispatch_semaphore_wait) is located obtains the semaphore, then it continues to execute downward and decrements the semaphore by 1.

  If the semaphore is not acquired during the waiting period or the value of the semaphore is always 0, then when the timeout is reached, the thread where it is located will automatically execute subsequent statements.

  

(4) The return value of dispatch_semaphore_signal is of type long. When the return value is 0, it means that there is currently no thread waiting for the semaphore to be processed.

  The value of the semaphore can be increased by 1. When the return value is not 0, it means that it currently has (one or more) threads waiting for the semaphore to be processed, and the function wakes up a

  A waiting thread (when the thread has a priority, wake up the thread with the highest priority; otherwise wake up randomly).

  The return value of dispatch_semaphore_wait is also of type long. When it returns 0, it means that the thread where the function is located was successfully woken up before the timeout.

  When its return is not 0, it means that the timeout occurred.

 

(5) When setting timeout, there are two useful macros: DISPATCH_TIME_NOW and DISPATCH_TIME_FOREVER.

  DISPATCH_TIME_NOW means current;

  DISPATCH_TIME_FOREVER represents the distant future;

  Generally, you can directly set timeout to one of these two macros, or create a variable of type dispatch_time_t yourself.

  There are two ways to create a variable of type dispatch_time_t, dispatch_time and dispatch_walltime.

  These two variables are generally used when creating dispatch_time_t type variables by creating dispatch_time.

  The declaration of dispatch_time is as follows:

  dispatch_time_t dispatch_time(dispatch_time_t when, int64_t delta);

  Its parameter when needs to pass in a variable of type dispatch_time_t and a delta value. Indicates that when plus delta time is the timeout time.

  例如:dispatch_time_t  t = dispatch_time(DISPATCH_TIME_NOW, 1*1000*1000*1000);

     Indicates that the current time is delayed by one second as the timeout time.

 

(6) Regarding the semaphore, parking can generally be used as an analogy.

  There are 4 parking spaces left in the parking lot, so even if four cars come at the same time, they can park. If five cars arrive at this time, then one needs to wait.

  The value of the semaphore is equivalent to the number of remaining parking spaces, the dispatch_semaphore_wait function is equivalent to a car coming, dispatch_semaphore_signal

  It's like walking a car. The remaining number of parking spaces has been specified during initialization (dispatch_semaphore_create(long value)),

  When dispatch_semaphore_signal is called once, the remaining parking space is increased by one; the remaining parking space is reduced by one when dispatch_semaphore_wait is called once;

  When the remaining parking space is 0, the next car (that is, calling dispatch_semaphore_wait) can only wait. It is possible that several vehicles are waiting for a parking space at the same time. some car owners

  I was impatient and set a waiting time for myself. During this time, I couldn't wait for a parking space and left. If I waited, I drove in and parked. And some car owners like to park their cars here,

  So just keep waiting.

 

(7) A simple example of the code is as follows:

 1 dispatch_semaphore_t signal; 2     signal = dispatch_semaphore_create(1); 3     __block long x = 0; 4     NSLog(@"0_x:%ld",x); 5     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 6         sleep(1); 7         NSLog(@"waiting"); 8         x = dispatch_semaphore_signal(signal); 9         NSLog(@"1_x:%ld",x);10  11         sleep(2);12         NSLog(@"waking");13         x = dispatch_semaphore_signal(signal);14         NSLog(@"2_x:%ld",x);15     });16 //    dispatch_time_t duration = dispatch_time(DISPATCH_TIME_NOW, 1*1000*1000*1000); //超时1秒17 //    dispatch_semaphore_wait(signal, duration);18  19     x = dispatch_semaphore_wait(signal, DISPATCH_TIME_FOREVER);20     NSLog(@"3_x:%ld",x);21  22     x = dispatch_semaphore_wait(signal, DISPATCH_TIME_FOREVER);23     NSLog(@"wait 2");24     NSLog(@"4_x:%ld",x);25  26     x = dispatch_semaphore_wait(signal, DISPATCH_TIME_FOREVER);27     NSLog(@"wait 3");28     NSLog(@"5_x:%ld",x);

 

The final printed result is:

 1 2014-08-11 22:51:54.734 LHTest[15700:70b] 0_x:0 2 2014-08-11 22:51:54.737 LHTest[15700:70b] 3_x:0 3 2014-08-11 22:51:55.738 LHTest[15700:f03] waiting 4 2014-08-11 22:51:55.739 LHTest[15700:70b] wait 2 5 2014-08-11 22:51:55.739 LHTest[15700:f03] 1_x:1 6 2014-08-11 22:51:55.739 LHTest[15700:70b] 4_x:0 7 2014-08-11 22:51:57.741 LHTest[15700:f03] waking 8 2014-08-11 22:51:57.742 LHTest[15700:f03] 2_x:1 9 2014-08-11 22:51:57.742 LHTest[15700:70b] wait 310 2014-08-11 22:51:57.742 LHTest[15700:70b] 5_x:0

 

Original address: Please indicate the source for reprint http://www.cnblogs.com/snailHL/p/3906112.html

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324671603&siteId=291194637