Start three threads A, B, C and print 10 times in the order ABC

1. OC uses NSLock


    NSLock *lockA = [[NSLock alloc] init];
    NSLock *lockB = [[NSLock alloc] init];
    NSLock *lockC = [[NSLock alloc] init];
    
    [lockB lock];
    [lockC lock];
    
    dispatch_queue_t queueA = dispatch_queue_create("queuea", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queueA, ^{
        for (int i = 0; i<10; i++) {
            [lockA lock];
            NSLog(@"A======= %@",@(i)); [lockB unlock]; }}); dispatch_queue_t queueB = dispatch_queue_create("queueb", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queueB, ^{
        for (int i = 0; i<10; i++) {
            [lockB lock];
             NSLog(@"B======= %@",@(i)); [lockC unlock]; }}); dispatch_queue_t queueC = dispatch_queue_create("queuec", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queueC, ^{
        for (int i = 0; i<10; i++) {
            [lockC lock];
             NSLog(@"C======= %@",@(i));
             NSLog(@""); [lockA unlock]; }});Copy the code

Output:

A======= 0 2019-06-20 01:06:23.384170+0800 xxtest[2808:669702] B======= 0 2019-06-20 01:06:23.384515+0800 xxtest[2808:669703] C======= 0 2019-06-20 01:06:23.384613+0800 Xxtest [2808:669703] A======= 1 cialis [2808:669702] 1 cialis [2808:669701] B======= 1 cialis [2808:669703] C======= 1 cialis [808:669702] A======= 2 cialis [808:669702 01:06:23.386590+0800 xxtest[2808:669701] B======= 2 2019-06-20 01:06:23.387100+0800 xxtest[2808:669703] C======= 2 Cialis [808:669702] A======= 3 cialis [808:669702] cialis [808:669702 B======= 3 cialis [580:669703] C======= 3 2019-06-20 01:06:23.388969+0800 xxtest[2808:669703] 2019-06-20 01:06:23.389857+0800 xxtest[2808:669702] A======= 4 2019-06-20 01:06:23.390137+0800 xxtest[2808:669701] B======= 4 2019-06-20 01:06:23.390546+0800 Xxtest [2808:669703] C======= 4 cialis [2808:669703] 4 cialis Xxtest [2808:669702] A======= 5 cialis [2808:669702] B======= 5 cialis C======= 5 cialis [808:669703] cialis [808:669703 01:06:23.392443+0800 xxtest[2808:669702] A======= 6 2019-06-20 01:06:23.392804+0800 xxtest[2808:669701] B======= 6 Cialis [580:669703] C======= 6 cialis [580:669703] A======= 7 cialis [2808:669702] 5 cialis [2808:669702] B======= 7 2019-06-20 01:06:23.394301+0800 xxtest[2808:669703] C======= 7 2019-06-20 01:06:23.394578+0800 Xxtest [2808:669703] A======= 8 cialis [2808:669702 Xxtest [2808:669701] B======= 8 2019-06-20 01:06:23.395873+0800 xxtest[2808:669703] C======= 8 2019-06-20 [808:669702] A======= 9 cialis [808:669702 B======= 9 2019-06-20 01:06:23.39797 +0800 xxtest[2808:669701] C======= 9 The 2019-06-20 01:06:23. 397649 + 0800 xxtest (2808-669703)Copy the code

2. OC Dispatch_semaphore is used

 dispatch_semaphore_t semaA = dispatch_semaphore_create(1);
    dispatch_semaphore_t semaB = dispatch_semaphore_create(0);
    dispatch_semaphore_t semaC = dispatch_semaphore_create(0);
    
    
    dispatch_queue_t queueA = dispatch_queue_create("queuea", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queueA, ^{
        for (int i = 0; i<10; i++) {
            dispatch_semaphore_wait(semaA, DISPATCH_TIME_FOREVER);
            NSLog(@"A======= %@",@(i)); dispatch_semaphore_signal(semaB); }}); dispatch_queue_t queueB = dispatch_queue_create("queueb", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queueB, ^{
        for (int i = 0; i<10; i++) {
            dispatch_semaphore_wait(semaB, DISPATCH_TIME_FOREVER);
            NSLog(@"B======= %@",@(i)); dispatch_semaphore_signal(semaC); }}); dispatch_queue_t queueC = dispatch_queue_create("queuec", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queueC, ^{
        for (int i = 0; i<10; i++) {
            dispatch_semaphore_wait(semaC, DISPATCH_TIME_FOREVER);
            NSLog(@"C======= %@",@(i));
            NSLog(@""); dispatch_semaphore_signal(semaA); }});Copy the code

Output:

Cialis [2938:708288] A======= 0 cialis [2938:708288] B======= 0 cialis [2938:708286] C======= 0 cialis [2938:708286] A======= 1 cialis [2938:708288] 1 cialis [2938:708288 [2938:708285] B======= 1 cialis [2938:708286] C======= 1 cialis [2938:708286] A======= 2 cialis [2938:708288 B======= 2 2019-06-20 01:40:25.051442+0800 xxtest[2938:708286] C======= 2 Cialis [2938:708286] A======= 3 [2938:708288] cialis [2938:708288] A======= 3 Cialis [2938:708285] B======= 3 cialis [2938:708285] C======= 3 cialis [2938:708286] cialis [2938:708288] A======= 4 2019-06-20 01:40:25.053340+0800 xxtest[2938:708285] B======= 4 2019-06-20 01:40:25.053617+0800 [2938:708286] C======= 4 cialis [2938:708286] 4 cialis [2938:708286 [2938:708288] A======= 5 cialis [2938:708285] B======= 5 cialis [2938:708286] C======= 5 cialis [2938:708286] cialis [2938:708286 A======= 6 2019-06-20 01:40:25.05588 +0800 xxtest[2938:708288] B======= 6 Cialis [2938:708286] C======= 6 cialis [2938:708286] [2938:708288] A======= 7 cialis [2938:708288] B======= 7 2019-06-20 01:40:25.057789+0800 xxtest[2938:708286] C======= 7 2019-06-20 01:40:25.058204+0800 [2938:708286] A======= 8 cialis [2938:708288] 5 cialis [2938:708288 Xxtest [2938:708285] B======= 8 2019-06-20 01:40:25.059211+0800 xxtest[2938:708286] C======= 8 2019-06-20 [2938:708286] A======= 9 cialis [2938:708288 B======= 9 2019-06-20 01:40:25.060360+0800 xxtest[2938:708285] C======= 9 The 2019-06-20 01:40:25. 060585 + 0800 xxtest (2938-708286)Copy the code