根据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);
控制台将在我们打印唤醒时间: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而崩溃?
谢谢!
答案 0 :(得分:0)
你解决了吗?我面临同样的问题;
我觉得觉醒异常不是崩溃的真正原因;因为我写了一个演示,像你一样重复这个崩溃,当演示运行时,我自己退出演示,这个日志也会被生成,也是异常子类型:WAKEUPS,所以我认为这是其他一些原因,可能是记忆太高了;