iOS 判断当前线程是否为主线程

2,464 阅读1分钟

if (strcmp(dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL), dispatch_queue_get_label(dispatch_get_main_queue())) == 0) { //running on main thread NSLog(@"当前为主线程")

}else{
    NSLog(@"当前为其他线程");
    dispatch_async(dispatch_get_main_queue(),
                   ^{
                        NSLog(@"加入主线程")
                   });
}