// 创建一个信号量
dispatch_semaphore_t xl_semaphore = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
// 发送网络请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
// 添加信号量
dispatch_semaphore_signal(xl_semaphore);
}];
#pragma clang diagnostic pop
// 等待信号的到来
dispatch_semaphore_wait(xl_semaphore, DISPATCH_TIME_FOREVER);
dispatch_async(dispatch_get_main_queue(), ^{
// 在主线程中做操作
});
});
GCD多线程dispatch_semaphore_t的使用
最新推荐文章于 2022-08-22 16:43:04 发布