楼主这种研究实践的精神值得学习。可以肯定这个[NSThread isMainThread]方法是可靠的。dispatch_async 表示异步 ,肯定是子线程里执行的。
dispatch_sync  表示同步,  其实就是放在主线程来阻塞。

解决方案 »

  1.   


    -(void) test
    {
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            
            BOOL bIsMain = [NSThread isMainThread];
            NSLog(@"AAA    bIsMain = %d", bIsMain);
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                BOOL bIsMain = [NSThread isMainThread];
                NSLog(@"BBB  bIsMain = %d", bIsMain);
                
            });
            
            dispatch_queue_t queue = dispatch_queue_create("gcd1", NULL);
            
            dispatch_sync(queue, ^{
                
                BOOL bIsMain = [NSThread isMainThread];
                NSLog(@"CCC    bIsMain = %d", bIsMain);
                
                
                dispatch_queue_t queue2 = dispatch_queue_create("gcd2", NULL);
                dispatch_sync(queue2, ^{
                    
                    BOOL bIsMain = [NSThread isMainThread];
                    NSLog(@"DDDD  bIsMain = %d", bIsMain);
                    
                });
                
            });
            
            dispatch_sync(dispatch_get_global_queue(0, 0), ^{
                
                BOOL bIsMain = [NSThread isMainThread];
                NSLog(@"EEEEE    bIsMain = %d", bIsMain);
                
                dispatch_sync(dispatch_get_main_queue(), ^{
                    
                    BOOL bIsMain = [NSThread isMainThread];
                    NSLog(@"FFFFF  bIsMain = %d", bIsMain);
                    
                });
                
            });
            
        });
    }
      

  2.   

    谢谢回复。可是,虽然是同步,但是是放在新建的queue上执行的啊(不是主线程的queue)?难道我理解错了?  同步都是在主线程中执行,异步都是在子线程中执行?如果是这样,那怎么解释异步在主线程队列中执行呢(如下代码,log显示主线程)?    dispatch_async(dispatch_get_main_queue(), ^{
            BOOL isMain = [NSThread isMainThread];
            
            if (isMain) {
                NSLog(@"---主线程---");
            }else{
                NSLog(@"---子线程---");
            }
        });
      

  3.   


    dispatch_sync(),同步添加操作。他是等待添加进队列里面的block操作完成之后再继续执行。dispatch_async() ,异步添加block进任务队列,它不会做任何等待。苹果文档中:dispatch_async:
    This function is the fundamental mechanism for submitting blocks to a dispatch queue. Calls to this function always return immediately after the block has been submitted and never wait for the block to be invoked. The target queue determines whether the block is invoked serially or concurrently with respect to other blocks submitted to that same queue. Independent serial queues are processed concurrently with respect to each other.dispatch_sync:
    Submits a block to a dispatch queue for synchronous execution. Unlike dispatch_async, this function does not return until the block has finished. Calling this function and targeting the current queue results in deadlock.
    Unlike with dispatch_async, no retain is performed on the target queue. Because calls to this function are synchronous, it "borrows" the reference of the caller. Moreover, no Block_copy is performed on the block.
    As an optimization, this function invokes the block on the current thread when possible.
    某有说明这两个操作与主线程有什么关系,只有提及跟队列有关联。
      

  4.   


    我执行了你的代码。 CCC是0,在我的demo里是1,能告诉我为什么吗?感觉我不太明白。
    外层代码都是
        dispatch_queue_t queue = dispatch_queue_create("gcd1", NULL);
        dispatch_sync(queue, ^{});
      

  5.   

    这跟你那段代码运行位置有关系
    dispatch_queue_t queue = dispatch_queue_create("gcd1", NULL);
        dispatch_sync(queue, ^{        //1
            BOOL isMain = [NSThread isMainThread];
             
            if (isMain) {
                NSLog(@"---主线程---");
            }else{
                NSLog(@"---子线程---");
            }
        });
    在主线程中执行就打印主线程,,在子线程中就打印子线程
    你可以把你这代码放到我写那段代码里面,任意位置看下打印信息。
      

  6.   


    抛到
    dispatch_get_main_queue()
    队列里面的block,是在主线程中执行的!
      

  7.   


    - (void)fun {
        dispatch_queue_t queue = dispatch_queue_create("gcd1", NULL);
        dispatch_sync(queue, ^{
            BOOL isMain = [NSThread isMainThread];
            
            if (isMain) {
                NSLog(@"---主线程---");
            }else{
                NSLog(@"---子线程---");
            }
        });
    }
    //执行结果:
    //主线程执行fun(),log显示主
    //子线程执行fun(),log显示子谢啦。但还是没能解释:为什么log输出值和fun 在哪(同步或异步) 执行一致,而和queue无关。
      

  8.   


    这个解释到位。
    如果在主线程里运行下面的代码。 
    那就有四种情况:
       dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            BOOL isMain = [NSThread isMainThread];
            if (isMain) {
                NSLog(@"---主线程---");
            }else{
                NSLog(@"---子线程---");
            }
        });这段代码打印:---子线程---   dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            BOOL isMain = [NSThread isMainThread];
            if (isMain) {
                NSLog(@"---主线程---");
            }else{
                NSLog(@"---子线程---");
            }
        });这段代码打印:---主线程---
        dispatch_async(dispatch_get_main_queue(), ^{
            BOOL isMain = [NSThread isMainThread];
            
            if (isMain) {
                NSLog(@"---主线程---");
            }else{
                NSLog(@"---子线程---");
            }
        });这段代码打印:---主线程---    
        dispatch_sync(dispatch_get_main_queue(), ^{
            BOOL isMain = [NSThread isMainThread];
            
            if (isMain) {
                NSLog(@"---主线程---");
            }else{
                NSLog(@"---子线程---");
            }
        });阻塞主线程,不打印
      

  9.   


    这个解释到位。
    如果在主线程里运行下面的代码。 
    那就有四种情况:
       dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            BOOL isMain = [NSThread isMainThread];
            if (isMain) {
                NSLog(@"---主线程---");
            }else{
                NSLog(@"---子线程---");
            }
        });这段代码打印:---子线程---   dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            BOOL isMain = [NSThread isMainThread];
            if (isMain) {
                NSLog(@"---主线程---");
            }else{
                NSLog(@"---子线程---");
            }
        });这段代码打印:---主线程---
        dispatch_async(dispatch_get_main_queue(), ^{
            BOOL isMain = [NSThread isMainThread];
            
            if (isMain) {
                NSLog(@"---主线程---");
            }else{
                NSLog(@"---子线程---");
            }
        });这段代码打印:---主线程---    
        dispatch_sync(dispatch_get_main_queue(), ^{
            BOOL isMain = [NSThread isMainThread];
            
            if (isMain) {
                NSLog(@"---主线程---");
            }else{
                NSLog(@"---子线程---");
            }
        });阻塞主线程,不打印
    非常感谢!