我在一些非主线程中使用dispatch_async
。我们称之为“线程A”。我没有使用GCD获得线程A.在执行了一些块后,我想在线程A中返回。我该怎么做?
踏A:
dispatch_queue_t fetchQ = dispatch_queue_create("Thread B", NULL);
dispatch_async(fetchQ, ^{
// Do some stuff...
// ...
//Now I want to return in Thread A
});
dispatch_release(fetchQ);
答案 0 :(得分:0)
怎么样
dispatch_queue_t fetchA = ...
//..
dispatch_queue_t fetchQ = dispatch_queue_create("Thread B", NULL);
dispatch_async(fetchQ, ^{
// Do some stuff...
// ...
dispatch_async(fetchA, ^{
// Do stuff on Tread A
});
});
dispatch_release(fetchQ);
答案 1 :(得分:0)
这是一种丑陋的做法,但似乎有效。
dispatch_queue_t fetchQ = dispatch_queue_create("Thread B", NULL);
__block BOOL changeWhenComplete = NO;
dispatch_async(fetchQ, ^{
for (int i =0; i < 2; i++) {
NSLog(@"doing work");
sleep(1);
}
//Now I want to return in Thread A
changeWhenComplete = YES;
});
dispatch_release(fetchQ);
while (!changeWhenComplete) {
sleep(1);
NSLog(@"waiting for fetchQ to complete");
}
NSLog(@"fetchQ returned");
return;
值得注意的是,这将阻止线程A.