[NSThread isMainThread]返回值可靠吗

zioc2014 2014-11-12 04:16:49

dispatch_queue_t queue = dispatch_queue_create("gcd1", NULL);
dispatch_async(queue, ^{ //1
BOOL isMain = [NSThread isMainThread];

if (isMain) {
NSLog(@"---主线程---");
}else{
NSLog(@"---子线程---");
}
});

执行后,log显示:子线程
将1处改成dispatch_sync,log显示:主线程-----表示不理解

更改后不应该还是在子线程上运行的么(只不过是同步)?
...全文
407 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
zioc2014 2014-11-13
  • 打赏
  • 举报
回复
引用 11 楼 totogo2010 的回复:
[quote=引用 8 楼 hslinux 的回复:] 这跟你那段代码运行位置有关系

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

   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(@"---子线程---");
        }
    });
阻塞主线程,不打印[/quote] 非常感谢!
空杯子_ 2014-11-12
  • 打赏
  • 举报
回复
引用 8 楼 hslinux 的回复:
这跟你那段代码运行位置有关系

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

   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(@"---子线程---");
        }
    });
阻塞主线程,不打印
zioc2014 2014-11-12
  • 打赏
  • 举报
回复
引用 9 楼 hslinux 的回复:
抛到 dispatch_get_main_queue() 队列里面的block,是在主线程中执行的!

- (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无关。
hslinux 2014-11-12
  • 打赏
  • 举报
回复
抛到 dispatch_get_main_queue() 队列里面的block,是在主线程中执行的!
hslinux 2014-11-12
  • 打赏
  • 举报
回复
这跟你那段代码运行位置有关系

dispatch_queue_t queue = dispatch_queue_create("gcd1", NULL);
    dispatch_sync(queue, ^{        //1
        BOOL isMain = [NSThread isMainThread];
         
        if (isMain) {
            NSLog(@"---主线程---");
        }else{
            NSLog(@"---子线程---");
        }
    });
在主线程中执行就打印主线程,,在子线程中就打印子线程 你可以把你这代码放到我写那段代码里面,任意位置看下打印信息。
zioc2014 2014-11-12
  • 打赏
  • 举报
回复
引用 5 楼 hslinux 的回复:
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. 某有说明这两个操作与主线程有什么关系,只有提及跟队列有关联。
同步异步我清楚,他们和是否是主线程/子线程没有直接关系吧? 我以为 线程和队列是对应的,主队列就是主线程在做,现在调试好像又貌似不是这样。。
zioc2014 2014-11-12
  • 打赏
  • 举报
回复
引用 3 楼 hslinux 的回复:

-(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);
                
            });
            
        });
        
    });
}

我执行了你的代码。 CCC是0,在我的demo里是1,能告诉我为什么吗?感觉我不太明白。 外层代码都是 dispatch_queue_t queue = dispatch_queue_create("gcd1", NULL); dispatch_sync(queue, ^{});
hslinux 2014-11-12
  • 打赏
  • 举报
回复
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. 某有说明这两个操作与主线程有什么关系,只有提及跟队列有关联。
zioc2014 2014-11-12
  • 打赏
  • 举报
回复
引用 1 楼 zhanglei5415 的回复:
同步线程就是在主线程上运行的。它的执行会阻塞主线程。异步线程是在子线程中来执行的,可以用异步线程来处理一些比较耗时的操作。
引用 2 楼 totogo2010 的回复:
楼主这种研究实践的精神值得学习。 可以肯定这个[NSThread isMainThread]方法是可靠的。 dispatch_async 表示异步 ,肯定是子线程里执行的。 dispatch_sync 表示同步, 其实就是放在主线程来阻塞。
谢谢回复。 可是,虽然是同步,但是是放在新建的queue上执行的啊(不是主线程的queue)? 难道我理解错了? 同步都是在主线程中执行,异步都是在子线程中执行?如果是这样,那怎么解释异步在主线程队列中执行呢(如下代码,log显示主线程)?

    dispatch_async(dispatch_get_main_queue(), ^{
        BOOL isMain = [NSThread isMainThread];
        
        if (isMain) {
            NSLog(@"---主线程---");
        }else{
            NSLog(@"---子线程---");
        }
    });
hslinux 2014-11-12
  • 打赏
  • 举报
回复

-(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);
                
            });
            
        });
        
    });
}

空杯子_ 2014-11-12
  • 打赏
  • 举报
回复
楼主这种研究实践的精神值得学习。 可以肯定这个[NSThread isMainThread]方法是可靠的。 dispatch_async 表示异步 ,肯定是子线程里执行的。 dispatch_sync 表示同步, 其实就是放在主线程来阻塞。
ReyZhang 2014-11-12
  • 打赏
  • 举报
回复
同步线程就是在主线程上运行的。它的执行会阻塞主线程。异步线程是在子线程中来执行的,可以用异步线程来处理一些比较耗时的操作。

29,049

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧