IOS移动开发技术前沿iOS开发记录

被遗弃的线程

2018-04-14  本文已影响579人  sindri的小巢
image

原文地址

main函数作为程序运行的入口,正常情况下,函数会执行毫秒级别的操作,然后返回一个0表示程序正常终止。为了避免应用启动即终止,苹果设计了runloop机制来维持线程的生命,runloop在每一次的循环当中不断的去处理事件,或控制线程的休眠和唤醒。runloop还结合了libdispatch的任务派发机制,可以循环地处理async到队列中的任务

启动runloop

runloop对外暴露的接口来看,启动方式一共存在三种:

如果runloop在启动之后没有任何sourcestimers或者ports事件可以处理,那么会自动退出,否则会在处理完成后让线程陷入休眠,等待这些事件重新唤醒线程处理。下面是最常用来表示runloop处理逻辑的示意图:

image

除开图中列出的事件之外,main loop会处理timer之后检测队列中是否存在待执行的block然后开始执行

什么情况下需要启动runloop

主线程的runloop会在应用启动后被UIApplication启动,其他线程则需要我们主动去run。从接触iOS开发到现在,笔者了解的需要主动启动runloop只有这么两类:

上面两种不同的应用场景,实际上是使用timerport维持runloop不会因为没有事件处理直接退出,而且在这些源事件来临之前,线程大多数情况下处在休眠状态不造成额外损耗

串行队列的runloop

假设现在需要使用一个子线程的runloop来实现定时器,由于runloop在停止之前,线程会一直存活,因此可能会想利用这个存活的线程处理其他的任务。因此除了NSTimer之外,我们添加一个GCD Timer定时的派发任务给这个启动runloop的队列:

dispatch_queue_t serialQueue = dispatch_queue_create("serial.queue", DISPATCH_QUEUE_SERIAL);

dispatch_async(serialQueue, ^{
    NSLog(@"the task run in the thread: %d", mach_thread_self());
    [NSTimer scheduledTimerWithTimeInterval: 0.5 repeats: YES block: ^(NSTimer * _Nonnull timer) {
        NSLog(@"ns timer in the thread: %d", mach_thread_self());
    }];
    [[NSRunLoop currentRunLoop] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 600]];
});

dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, serialQueue);
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
dispatch_source_set_event_handler(timer, ^{
    NSLog(@"gcd timer in the thread: %d", mach_thread_self());
});
dispatch_resume(timer);

按照预期,这段代码充分利用了已经被保活的线程,除了已有的NSTimer之外,线程还能在空闲的时间去处理不断派发的任务,但实际上只有NSTimer的任务被执行:

2018-04-14 10:32:54.667718+0800 PThreads[7693:94702] the task run in the thread: 4355
2018-04-14 10:32:55.168871+0800 PThreads[7693:94702] ns timer in the thread: 4355
2018-04-14 10:32:55.672011+0800 PThreads[7693:94702] ns timer in the thread: 4355
2018-04-14 10:32:56.169150+0800 PThreads[7693:94702] ns timer in the thread: 4355
2018-04-14 10:32:56.669411+0800 PThreads[7693:94702] ns timer in the thread: 4355
2018-04-14 10:32:57.169665+0800 PThreads[7693:94702] ns timer in the thread: 4355
2018-04-14 10:32:57.669234+0800 PThreads[7693:94702] ns timer in the thread: 4355
2018-04-14 10:32:58.172068+0800 PThreads[7693:94702] ns timer in the thread: 4355
2018-04-14 10:32:58.669446+0800 PThreads[7693:94702] ns timer in the thread: 4355
2018-04-14 10:32:59.169223+0800 PThreads[7693:94702] ns timer in the thread: 4355
2018-04-14 10:32:59.671802+0800 PThreads[7693:94702] ns timer in the thread: 4355

导致保活线程无法处理async任务的原因有两个:

为了证明这些原因,可以通过CFRunLoopPerformBlock将任务直接加入到runloop自身的任务队列中,检测这个任务是否被执行:

__block CFRunLoopRef serialRunLoop = NULL;
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_queue_t serialQueue = dispatch_queue_create("serial.queue", DISPATCH_QUEUE_SERIAL);

dispatch_async(serialQueue, ^{
    NSLog(@"the task run in the thread: %d", mach_thread_self());
    [NSTimer scheduledTimerWithTimeInterval: 0.5 repeats: YES block: ^(NSTimer * _Nonnull timer) {
        NSLog(@"ns timer in the thread: %d", mach_thread_self());
    }];
    serialRunLoop = [NSRunLoop currentRunLoop].getCFRunLoop;
    [[NSRunLoop currentRunLoop] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 600]];
});

dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, mainQueue);
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
dispatch_source_set_event_handler(timer, ^{
    dispatch_async(serialQueue, ^{
        NSLog(@"gcd timer in the thread: %d", mach_thread_self());
    });
    CFRunLoopPerformBlock(serialRunLoop, NSDefaultRunLoopMode, ^{
        NSLog(@"perform block in thread: %d", mach_thread_self());
    });
});
dispatch_resume(timer);

再次运行之后,async的任务依旧无法被处理,但是perform block的任务总是能在timer唤醒休眠的线程后被处理:

2018-04-14 11:01:29.925924+0800 PThreads[15619:198121] the task run in the thread: 4355
2018-04-14 11:01:30.428175+0800 PThreads[15619:198121] ns timer in the thread: 4355
2018-04-14 11:01:30.428982+0800 PThreads[15619:198121] perform block in thread: 4355
2018-04-14 11:01:30.429410+0800 PThreads[15619:198121] perform block in thread: 4355
2018-04-14 11:01:30.932411+0800 PThreads[15619:198121] ns timer in the thread: 4355
2018-04-14 11:01:30.932674+0800 PThreads[15619:198121] perform block in thread: 4355
2018-04-14 11:01:31.430281+0800 PThreads[15619:198121] ns timer in the thread: 4355
2018-04-14 11:01:31.430546+0800 PThreads[15619:198121] perform block in thread: 4355
2018-04-14 11:01:31.929485+0800 PThreads[15619:198121] ns timer in the thread: 4355
2018-04-14 11:01:31.929691+0800 PThreads[15619:198121] perform block in thread: 4355
2018-04-14 11:01:32.432369+0800 PThreads[15619:198121] ns timer in the thread: 4355
2018-04-14 11:01:32.432726+0800 PThreads[15619:198121] perform block in thread: 4355
2018-04-14 11:01:32.930981+0800 PThreads[15619:198121] ns timer in the thread: 4355
2018-04-14 11:01:32.931179+0800 PThreads[15619:198121] perform block in thread: 4355
2018-04-14 11:01:33.429207+0800 PThreads[15619:198121] ns timer in the thread: 4355
2018-04-14 11:01:33.429519+0800 PThreads[15619:198121] perform block in thread: 4355

并行队列的runloop

队列的串并行属性决定了队列能不能被多个线程处理任务,因此同样的代码在并行队列执行,产生的结果必然是有所区别的:

__block CFRunLoopRef serialRunLoop = NULL;
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_queue_t serialQueue = dispatch_queue_create("concurrent.queue", DISPATCH_QUEUE_CONCURRENT);

dispatch_async(serialQueue, ^{
    NSLog(@"the task run in the thread: %d", mach_thread_self());
    [NSTimer scheduledTimerWithTimeInterval: 0.5 repeats: YES block: ^(NSTimer * _Nonnull timer) {
        NSLog(@"ns timer in the thread: %d", mach_thread_self());
    }];
    serialRunLoop = [NSRunLoop currentRunLoop].getCFRunLoop;
    [[NSRunLoop currentRunLoop] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 600]];
});

dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, mainQueue);
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
dispatch_source_set_event_handler(timer, ^{
    dispatch_async(serialQueue, ^{
        NSLog(@"gcd timer in the thread: %d", mach_thread_self());
    });
    CFRunLoopPerformBlock(serialRunLoop, NSDefaultRunLoopMode, ^{
        NSLog(@"perform block in thread: %d", mach_thread_self());
    });
});
dispatch_resume(timer);

输出结果如下:

2018-04-14 11:02:47.348083+0800 PThreads[15988:203200] the task run in the thread: 4867
2018-04-14 11:02:47.397174+0800 PThreads[15988:203203] gcd timer in the thread: 3843
2018-04-14 11:02:47.840678+0800 PThreads[15988:203206] gcd timer in the thread: 9219
2018-04-14 11:02:47.852870+0800 PThreads[15988:203200] ns timer in the thread: 4867
2018-04-14 11:02:47.853122+0800 PThreads[15988:203200] perform block in thread: 4867
2018-04-14 11:02:47.853552+0800 PThreads[15988:203200] perform block in thread: 4867
2018-04-14 11:02:48.340602+0800 PThreads[15988:203206] gcd timer in the thread: 9219
2018-04-14 11:02:48.352863+0800 PThreads[15988:203200] ns timer in the thread: 4867
2018-04-14 11:02:48.353149+0800 PThreads[15988:203200] perform block in thread: 4867
2018-04-14 11:02:48.840085+0800 PThreads[15988:203206] gcd timer in the thread: 9219
2018-04-14 11:02:48.853918+0800 PThreads[15988:203200] ns timer in the thread: 4867
2018-04-14 11:02:48.854120+0800 PThreads[15988:203200] perform block in thread: 4867
2018-04-14 11:02:49.340729+0800 PThreads[15988:203206] gcd timer in the thread: 9219
2018-04-14 11:02:49.354172+0800 PThreads[15988:203200] ns timer in the thread: 4867
2018-04-14 11:02:49.354470+0800 PThreads[15988:203200] perform block in thread: 4867
2018-04-14 11:02:49.840661+0800 PThreads[15988:203206] gcd timer in the thread: 9219
2018-04-14 11:02:49.853115+0800 PThreads[15988:203200] ns timer in the thread: 4867
2018-04-14 11:02:49.853288+0800 PThreads[15988:203200] perform block in thread: 4867
2018-04-14 11:02:50.340078+0800 PThreads[15988:203206] gcd timer in the thread: 9219
2018-04-14 11:02:50.354262+0800 PThreads[15988:203200] ns timer in the thread: 4867
2018-04-14 11:02:50.354537+0800 PThreads[15988:203200] perform block in thread: 4867
2018-04-14 11:02:50.840653+0800 PThreads[15988:203206] gcd timer in the thread: 9219
2018-04-14 11:02:50.854117+0800 PThreads[15988:203200] ns timer in the thread: 4867
2018-04-14 11:02:50.854406+0800 PThreads[15988:203200] perform block in thread: 4867
2018-04-14 11:02:51.339972+0800 PThreads[15988:203206] gcd timer in the thread: 9219
2018-04-14 11:02:51.353246+0800 PThreads[15988:203200] ns timer in the thread: 4867
2018-04-14 11:02:51.353472+0800 PThreads[15988:203200] perform block in thread: 4867
2018-04-14 11:02:51.839917+0800 PThreads[15988:203206] gcd timer in the thread: 9219

虽然并行队列的async功能并不会因为启动了runloop受到影响,但是可以发现如果不去保存runloop,这个保活的线程除了定时器能正常处理之外,其他时候不会再被GCD复用

使用port保活

如果不使用NSTimer这种稳定的唤醒机制来保活线程,而是采用port的方式,线程的表现是否依旧符合预期?

__block CFRunLoopRef serialRunLoop = NULL;
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_queue_t serialQueue = dispatch_queue_create("concurrent.queue", DISPATCH_QUEUE_CONCURRENT);

dispatch_async(serialQueue, ^{
    serialRunLoop = [NSRunLoop currentRunLoop].getCFRunLoop;
    [[NSRunLoop currentRunLoop] addPort: [NSPort new] forMode: NSDefaultRunLoopMode];
    [[NSRunLoop currentRunLoop] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 600]];
});

dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, mainQueue);
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
dispatch_source_set_event_handler(timer, ^{
    CFRunLoopPerformBlock(serialRunLoop, NSDefaultRunLoopMode, ^{
        NSLog(@"perform block in thread: %d", mach_thread_self());
    });
});
dispatch_resume(timer);

此时任务总是不会被处理。由于runloop需要被唤醒才能处理队列任务,而perform block只是单纯的添加任务,没有唤醒功能。为了线程能够继续执行任务,这时候还需要不断的wake up线程:

dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, mainQueue);
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
dispatch_source_set_event_handler(timer, ^{
    CFRunLoopPerformBlock(serialRunLoop, NSDefaultRunLoopMode, ^{
        NSLog(@"perform block in thread: %d", mach_thread_self());
    });
    CFRunLoopWakeUp(serialRunLoop);
});
dispatch_resume(timer);

/// 日志输出
2018-04-14 11:15:10.314064+0800 PThreads[19459:248764] perform block in thread: 2563
2018-04-14 11:15:10.763480+0800 PThreads[19459:248764] perform block in thread: 2563
2018-04-14 11:15:11.263651+0800 PThreads[19459:248764] perform block in thread: 2563
2018-04-14 11:15:11.763957+0800 PThreads[19459:248764] perform block in thread: 2563
2018-04-14 11:15:12.264006+0800 PThreads[19459:248764] perform block in thread: 2563
2018-04-14 11:15:12.763384+0800 PThreads[19459:248764] perform block in thread: 2563

结论

从测试来看,在子线程启动runloop并不是一个很明智的选择:这会导致线程保活期间被遗弃,失去了处理消息派发的能力,且无法响应其他线程的通信。其次,即便可以通过perform block来继续为保活线程添加任务处理,但在保活线程的runloop缺乏稳定的唤醒机制的情况下,还需要其他线程来提供唤醒能力,这增加了代码设计的成本,并且不会有额外的好处

上一篇 下一篇

猜你喜欢

热点阅读