在带有许多线程唤醒的 iOS 8 上并不总是显示 EXC_RESOURCE 崩溃?

Posted

技术标签:

【中文标题】在带有许多线程唤醒的 iOS 8 上并不总是显示 EXC_RESOURCE 崩溃?【英文标题】:EXC_RESOURCE crash not always shown on iOS 8 with many many thread wakeups? 【发布时间】:2014-12-21 01:41:24 【问题描述】:

根据App shutdown with EXC_RESOURCE, WAKEUPS exception on ios 8 GM和How do I wake up a sleeping pthread,我写了两个线程,一个用来唤醒另一个:

static void *thread1(void *)

    struct timeval now;
    struct timezone tz;

    static int64_t count = 0;
    while (gRun) 
        pthread_mutex_lock(&mutex);

        pthread_cond_wait(&cond, &mutex);

        gettimeofday(&now, &tz);
        printf("thread1 wakes up %lld times at %ld:%d\n", count++, now.tv_sec, now.tv_usec);

        pthread_mutex_unlock(&mutex);
    

    return NULL;


static void *thread2(void *)

    struct timeval now;
    struct timezone tz;

    static int64_t count = 0;
    while (gRun) 
        pthread_mutex_lock(&mutex);

        pthread_cond_signal(&cond);

        gettimeofday(&now, &tz);
        printf("thread2 wakes up %lld times at %ld:%d\n", count++, now.tv_sec, now.tv_usec);

        pthread_mutex_unlock(&mutex);

        struct timespec a;

        a.tv_sec  = 0;
        a.tv_nsec = 100;

        nanosleep( &a, NULL );
    

    return NULL;

当我创建两个线程时:

    pthread_mutex_init(&mutex, NULL);
    pthread_cond_init(&cond, NULL);

    pthread_t tid;
    pthread_create(&tid, NULL, thread1, NULL);
    pthread_create(&tid, NULL, thread2, NULL);

控制台会在 us:ns 格式时间打印唤醒时间,所以我可以计算一个线程唤醒了多少次——超过 1000 次,但应用程序只崩溃了一次:

Date/Time:           2014-10-25 16:12:01.187 +0800
Launch Time:         2014-10-25 16:11:54.118 +0800
OS Version:          iOS 8.0.2 (12A405)
Report Version:      105

Exception Type:  EXC_RESOURCE
Exception Subtype: WAKEUPS
Exception Message: (Limit 150/sec) Observed 6418/sec over 300 secs
Triggered by Thread:  6

Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0:
0   libsystem_kernel.dylib          0x33580518 mach_msg_trap + 20
1   libsystem_kernel.dylib          0x3358030c mach_msg + 36
2   CoreFoundation                  0x25799dc6 __CFRunLoopServiceMachPort + 142
3   CoreFoundation                  0x2579838c __CFRunLoopRun + 1012
4   CoreFoundation                  0x256e5dac CFRunLoopRunSpecific + 472
5   CoreFoundation                  0x256e5bbe CFRunLoopRunInMode + 102
6   GraphicsServices                0x2ca7104c GSEventRunModal + 132
7   UIKit                           0x28cb1a2c UIApplicationMain + 1436
8   EXC_RESOURCE_DEMO               0x00032df4 0x2c000 + 28148
9   libdyld.dylib                   0x334ceaac start + 0

Thread 1 name:  Dispatch queue: com.apple.libdispatch-manager
Thread 1:
0   libsystem_kernel.dylib          0x335802c8 kevent64 + 24
1   libdispatch.dylib               0x334a2ec4 _dispatch_mgr_invoke + 276
2   libdispatch.dylib               0x334a2bf6 _dispatch_mgr_thread$VARIANT$mp + 34

Thread 2:
0   libsystem_kernel.dylib          0x335949cc __workq_kernreturn + 8
1   libsystem_pthread.dylib         0x33610e9c _pthread_wqthread + 788
2   libsystem_pthread.dylib         0x33610b74 start_wqthread + 4

Thread 3:
0   libsystem_kernel.dylib          0x335949cc __workq_kernreturn + 8
1   libsystem_pthread.dylib         0x33610e9c _pthread_wqthread + 788
2   libsystem_pthread.dylib         0x33610b74 start_wqthread + 4

Thread 4:
0   libsystem_kernel.dylib          0x335949cc __workq_kernreturn + 8
1   libsystem_pthread.dylib         0x33610e9c _pthread_wqthread + 788
2   libsystem_pthread.dylib         0x33610b74 start_wqthread + 4

Thread 5:
0   libsystem_kernel.dylib          0x335949cc __workq_kernreturn + 8
1   libsystem_pthread.dylib         0x33610e9c _pthread_wqthread + 788
2   libsystem_pthread.dylib         0x33610b74 start_wqthread + 4

Thread 6 Attributed:
0   libsystem_kernel.dylib          0x33593b38 0x3357f000 + 84792
1   libsystem_pthread.dylib         0x336123dc 0x33610000 + 9180
2   libsystem_pthread.dylib         0x336132ac 0x33610000 + 12972
3   EXC_RESOURCE_DEMO               0x000328f8 0x2c000 + 26872
4   libsystem_pthread.dylib         0x33612e64 _pthread_body + 136
5   libsystem_pthread.dylib         0x33612dd6 _pthread_start + 114
6   libsystem_pthread.dylib         0x33610b80 thread_start + 4

所以,我的问题是,每秒唤醒这么多,大约 6000/秒,为什么应用程序只崩溃一次?我怎样才能让它总是因为 EXC_RESOURCE / WAKEUPS 而崩溃?

谢谢!

【问题讨论】:

【参考方案1】:

你解决了吗?我面临同样的问题;

我认为 wakeups Exception 并不是崩溃的真正原因;因为我像你一样写了一个demo来重现这个crash,demo运行的时候我自己退出demo,也会生成这个log,还有Exception Subtype: WAKEUPS,所以我觉得是这个原因,可能是其他原因内存太高了;

【讨论】:

这应该进入评论,而不是答案。 “我认为唤醒异常不是崩溃的真正原因”,你是对的。最后我发现真正的原因是insert nil

以上是关于在带有许多线程唤醒的 iOS 8 上并不总是显示 EXC_RESOURCE 崩溃?的主要内容,如果未能解决你的问题,请参考以下文章

将焦点放在 WPF ComboBox 上并不总是有效

iOS - 如何测量线程唤醒?

接近唤醒锁并不总是重新打开屏幕

iOS 8 推送通知操作按钮 - 当应用程序在后台时,handleActionWithIdentifier 中的代码并不总是运行

ios推送通知并不总是显示

带有许多按钮的 Xcode iPad UIActionSheet 无法正确显示 iOS7