GCDにおけるキューとタスクネストの組合せテスト

9932 ワード

以下を参照してください.
GCDは人を惑わせやすいいくつかの小さな問題です
GCDにおけるキューとタスクネストの組合せ分析
スレッドセキュリティArray
dispatch_async、dispatch_sync、dispatch_barrier_async、dispatch_barrier_sync

PS:  , 

1.シリアルキュー(SERIAL)


1)非同期ネスト非同期

-(void)serialAsyncAsync{
    dispatch_queue_t serialQueue = dispatch_queue_create("com.chenl.gcddemo", DISPATCH_QUEUE_SERIAL);
    dispatch_async(serialQueue, ^{
        NSLog(@"outside -- %@",[NSThread currentThread]);
        dispatch_async(serialQueue, ^{
            NSLog(@"inside -- %@",[NSThread currentThread]);
            for (int i=0; i<10; i++) {
                NSLog(@"inside -- %@",@(i));
            }
        });
        
        for (int i=0; i<10; i++) {
            NSLog(@"outside -- %@",@(i));
        }
        NSLog(@"outside end -- %@",[NSThread currentThread]);
    });
}

結果:
2019-04-08 11:35:46.614228+0800 GCDDemo[4091:214368] outside -- {number = 3, name = (null)}
2019-04-08 11:35:46.614478+0800 GCDDemo[4091:214368] outside -- 0
2019-04-08 11:35:46.614588+0800 GCDDemo[4091:214368] outside -- 1
...
2019-04-08 11:35:46.615971+0800 GCDDemo[4091:214368] outside -- 9
2019-04-08 11:35:46.616262+0800 GCDDemo[4091:214368] outside end -- {number = 3, name = (null)}
2019-04-08 11:35:46.616494+0800 GCDDemo[4091:214368] inside -- {number = 3, name = (null)}
2019-04-08 11:35:46.616695+0800 GCDDemo[4091:214368] inside -- 0
2019-04-08 11:35:46.617105+0800 GCDDemo[4091:214368] inside -- 1
...
2019-04-08 11:35:46.621248+0800 GCDDemo[4091:214368] inside -- 9

2)非同期ネスト同期(ERROR)

-(void)serialAsyncSync{
    dispatch_queue_t serialQueue = dispatch_queue_create("com.chenl.gcddemo", DISPATCH_QUEUE_SERIAL);
    dispatch_async(serialQueue, ^{
        NSLog(@"outside -- %@",[NSThread currentThread]);
        dispatch_sync(serialQueue, ^{
            NSLog(@"inside -- %@",[NSThread currentThread]);
            for (int i=0; i<10; i++) {
                NSLog(@"inside -- %@",@(i));
            }
        });
        
        for (int i=0; i<10; i++) {
            NSLog(@"outside -- %@",@(i));
        }
        NSLog(@"outside end -- %@",[NSThread currentThread]);
    });
}

結果:エラー

3)同期ネスト非同期

-(void)serialSyncAsync{
    dispatch_queue_t serialQueue = dispatch_queue_create("com.chenl.gcddemo", DISPATCH_QUEUE_SERIAL);
    dispatch_sync(serialQueue, ^{
        NSLog(@"outside -- %@",[NSThread currentThread]);
        dispatch_async(serialQueue, ^{
            NSLog(@"inside -- %@",[NSThread currentThread]);
            for (int i=0; i<10; i++) {
                NSLog(@"inside -- %@",@(i));
            }
        });
        
        for (int i=0; i<10; i++) {
            NSLog(@"outside -- %@",@(i));
        }
        NSLog(@"outside end -- %@",[NSThread currentThread]);
    });
}

結果:
2019-04-08 11:38:28.763058+0800 GCDDemo[4142:222924] outside -- {number = 1, name = main}
2019-04-08 11:38:28.763196+0800 GCDDemo[4142:222924] outside -- 0
2019-04-08 11:38:28.763270+0800 GCDDemo[4142:222924] outside -- 1
...
2019-04-08 11:38:28.763843+0800 GCDDemo[4142:222924] outside -- 9
2019-04-08 11:38:28.763984+0800 GCDDemo[4142:222924] outside end -- {number = 1, name = main}
2019-04-08 11:38:28.764156+0800 GCDDemo[4142:223088] inside -- {number = 3, name = (null)}
2019-04-08 11:38:28.764441+0800 GCDDemo[4142:223088] inside -- 0
2019-04-08 11:38:28.764633+0800 GCDDemo[4142:223088] inside -- 1
...
2019-04-08 11:38:28.769840+0800 GCDDemo[4142:223088] inside -- 9

4)ネスト同期の同期

-(void)serialSyncSync{
    dispatch_queue_t serialQueue = dispatch_queue_create("com.chenl.gcddemo", DISPATCH_QUEUE_SERIAL);
    dispatch_sync(serialQueue, ^{
        NSLog(@"outside -- %@",[NSThread currentThread]);
        dispatch_sync(serialQueue, ^{
            NSLog(@"inside -- %@",[NSThread currentThread]);
            for (int i=0; i<10; i++) {
                NSLog(@"inside -- %@",@(i));
            }
        });
        
        for (int i=0; i<10; i++) {
            NSLog(@"outside -- %@",@(i));
        }
        NSLog(@"outside end -- %@",[NSThread currentThread]);
    });
}

結果:エラー

結論:

 , , ; 。

2.パラレルキュー(CONCURRENT)


1)非同期ネスト非同期

-(void)currentAsyncAsync{
    dispatch_queue_t currentQueue = dispatch_queue_create("com.chenl.gcddemo", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(currentQueue, ^{
        NSLog(@"outside -- %@",[NSThread currentThread]);
        dispatch_async(currentQueue, ^{
            NSLog(@"inside -- %@",[NSThread currentThread]);
            for (int i=0; i<10; i++) {
                NSLog(@"inside -- %@",@(i));
            }
        });
        
        for (int i=0; i<10; i++) {
            NSLog(@"outside -- %@",@(i));
        }
        NSLog(@"outside end -- %@",[NSThread currentThread]);
    });
}

結果:予測不可能、無秩序

2)非同期ネスト同期

-(void)currentAsyncSync{
    dispatch_queue_t currentQueue = dispatch_queue_create("com.chenl.gcddemo", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(currentQueue, ^{
        NSLog(@"outside -- %@",[NSThread currentThread]);
        dispatch_sync(currentQueue, ^{
            NSLog(@"inside -- %@",[NSThread currentThread]);
            
            dispatch_sync(currentQueue, ^{
                NSLog(@"inside inside");
                for (int i=0; i<10; i++) {
                    NSLog(@"inside inside -- %@",@(i));
                }
            });
            
            for (int i=0; i<10; i++) {
                NSLog(@"inside -- %@",@(i));
            }
        });
        
        for (int i=0; i<10; i++) {
            NSLog(@"outside -- %@",@(i));
        }
        NSLog(@"outside end -- %@",[NSThread currentThread]);
    });
}

結果:insideからoutside
2019-04-08 15:22:32.998533+0800 GCDDemo[13049:500501] outside -- {number = 3, name = (null)}
2019-04-08 15:22:32.998799+0800 GCDDemo[13049:500501] inside -- {number = 3, name = (null)}
2019-04-08 15:22:32.998881+0800 GCDDemo[13049:500501] inside inside
2019-04-08 15:22:32.998989+0800 GCDDemo[13049:500501] inside inside -- 0
2019-04-08 15:22:32.999955+0800 GCDDemo[13049:500501] inside inside -- 1
...
2019-04-08 15:22:33.001734+0800 GCDDemo[13049:500501] inside inside -- 9
2019-04-08 15:22:33.001803+0800 GCDDemo[13049:500501] inside -- 0
2019-04-08 15:22:33.002653+0800 GCDDemo[13049:500501] inside -- 1
...
2019-04-08 15:22:33.008001+0800 GCDDemo[13049:500501] inside -- 9
2019-04-08 15:22:33.008077+0800 GCDDemo[13049:500501] outside -- 0
2019-04-08 15:22:33.008151+0800 GCDDemo[13049:500501] outside -- 1
...
2019-04-08 15:22:33.009867+0800 GCDDemo[13049:500501] outside -- 9
2019-04-08 15:22:33.010082+0800 GCDDemo[13049:500501] outside end -- {number = 3, name = (null)}

3)同期ネスト非同期

-(void)currentSyncAsync{
    dispatch_queue_t currentQueue = dispatch_queue_create("com.chenl.gcddemo", DISPATCH_QUEUE_CONCURRENT);
    dispatch_sync(currentQueue, ^{
        NSLog(@"outside -- %@",[NSThread currentThread]);
        dispatch_async(currentQueue, ^{
            NSLog(@"inside -- %@",[NSThread currentThread]);
            for (int i=0; i<10; i++) {
                NSLog(@"inside -- %@",@(i));
            }
        });
        
        for (int i=0; i<10; i++) {
            NSLog(@"outside -- %@",@(i));
        }
        NSLog(@"outside end -- %@",[NSThread currentThread]);
    });
}

結果:予測不可能、無秩序

4)ネスト同期の同期

-(void)currentSyncSync{
    dispatch_queue_t currentQueue = dispatch_queue_create("com.chenl.gcddemo", DISPATCH_QUEUE_CONCURRENT);
    dispatch_sync(currentQueue, ^{
        NSLog(@"outside -- %@",[NSThread currentThread]);
        dispatch_sync(currentQueue, ^{
            NSLog(@"inside -- %@",[NSThread currentThread]);
            for (int i=0; i<10; i++) {
                NSLog(@"inside -- %@",@(i));
            }
        });
        
        for (int i=0; i<10; i++) {
            NSLog(@"outside -- %@",@(i));
        }
        NSLog(@"outside end -- %@",[NSThread currentThread]);
    });
}

結果:insideからoutside
2019-04-08 15:24:15.335330+0800 GCDDemo[13082:506871] outside -- {number = 1, name = main}
2019-04-08 15:24:15.335629+0800 GCDDemo[13082:506871] inside -- {number = 1, name = main}
2019-04-08 15:24:15.335725+0800 GCDDemo[13082:506871] inside -- 0
2019-04-08 15:24:15.335796+0800 GCDDemo[13082:506871] inside -- 1
...
2019-04-08 15:24:15.336425+0800 GCDDemo[13082:506871] inside -- 9
2019-04-08 15:24:15.336583+0800 GCDDemo[13082:506871] outside -- 0
2019-04-08 15:24:15.336770+0800 GCDDemo[13082:506871] outside -- 1
...
2019-04-08 15:24:15.343693+0800 GCDDemo[13082:506871] outside -- 9
2019-04-08 15:24:15.343779+0800 GCDDemo[13082:506871] outside end -- {number = 1, name = main}

結論:

 , , 。

3.dispatch_barrier_asyncとdispatch_barrier_sync


barrierの役割は、barrierの前のタスクを実行してからbarrierの後のタスクを実行することです.