IOS-dispatch_semaphore and NSOperationQueue concurrency
Concurrency: run multiple tasks at the same time. It is also called "colopicity", which refers to the ability to process multiple synchronous activities. Parallel: two concurrent tasks occur simultaneously. Concurrency is not necessarily parallel, and concurrency does not necessarily occur at the same time. For example, two couriers are delivered to the destination respectively. There are two solutions: (1) one courier delivers two couriers to the destination respectively. (Concurrent) (2) two couriers send one courier to the destination at the same time. (Parallel) in iOS, it is often seen that there is a need for a method to wait until another method is executed and then perform corresponding processing, such as some network requests, the second request needs to be processed based on the return value of the previous request, so we cannot allow both requests to go to the request network at the same time. The following is a record of how to control concurrency through GCD and NSOperationQueue. The dispatch_semaphore semaphore is an integer value and has an initial count value. The semaphore usually supports two operations: Notification and wait. When the signal is notified, the Count value will increase. When the semaphore waits on the thread, the thread will be blocked if necessary until the signal is notified with a value greater than 0, then the thread will reduce the count and continue to work. Operations related to three semaphores in GCD: dispatch_semaphore_create semaphores create dispatch_semaphore_signal send notification dispatch_semaphore_wait semaphores wait for copying code _ block dispatch_semaphore_t sem = dispatch_semaphore_create (0 ); dispatch_queue_t queue = dispatch_queue_create ("testBlock", NULL); dispatch_async (queue, ^ {for (int I = 0; I <100; I ++) {NSLog (@ "I value: % d", I) ;} dispatch_semaphore_signal (sem) ;}); dispatch_semaphore_wait (sem, DISPATC H_TIME_FOREVER); for (int j = 0; j <10; j ++) {NSLog (@ "j value: % d", j );} the running result of the copied code is: NSOperationQueue without adding dependencies: copy the code NSOperationQueue * queue = [[NSOperationQueue alloc] init]; queue. maxConcurrentOperationCount = 10; NSBlockOperation * operation1 = [NSBlockOperation blockOperationWithBlock: ^ {for (int I = 0; I <1000; I ++) {NSLog (@ "execution concurrency queue 1: % d ", I) ;}}]; NSBlockOperation * operation2 = [NSBlockOperati On blockOperationWithBlock: ^ () {for (int I = 0; I <1500; I ++) {NSLog (@ "execution concurrency queue 2: % d ", i) ;}}]; [queue addOperation: operation1]; [queue addOperation: operation2]; copy the code running result: Two NSOperation statements are printed concurrently. After adding dependency control: copy the code NSOperationQueue * queue = [[NSOperationQueue alloc] init]; queue. maxConcurrentOperationCount = 10; NSBlockOperation * operation1 = [NSBlockOperation blockOperationWithBlock: ^ {for (int I = 0; I <1000; I ++) {NSLog (@ "execution concurrency queue 1: % d ", I) ;}}]; NSBlockOperation * operation2 = [NSBlockOperation blockOperationWithBlock: ^ () {for (int I = 0; I <1500; I ++) {NSLog (@ "execution concurrency queue 2: % d", I) ;}}]; [operation1 addDependency: operation2]; // Add dependency, operation1 depends on operation2 to continue [queue addOperation: operation1]; [queue addOperation: operation2];