Man*_*nny 4 multithreading join objective-c
是否有一种干净的方式来加入Objective C中的线程,就像Java中的"Thread.join"一样?我发现方法performSelector:onThread:withObject:waitUntilDone:但是这个限制是我不能在另一行上调用"阻塞",因为我想做这样的事情:
[dispatch Thread A];
[process something on main thread];
[wait for Thread A to finish before proceeding];
Run Code Online (Sandbox Code Playgroud)
先感谢您.
Dav*_*ong 12
我不知道有任何Cocoa API可以做到这一点,但它不会太难NSThread做到,使用锁很容易,甚至更容易使用Grand Central Dispatch.
NSThread
NSThread * otherThread = [[NSThread alloc] initWithTarget:self selector:@selector(methodToPerformInBackground:) object:aParameter];
[otherThread start];
//do some stuff
while ([otherThread isFinished] == NO) {
usleep(1000);
}
[otherThread release];
Run Code Online (Sandbox Code Playgroud)
NSLock
NSLock * lock = [[NSLock alloc] init];
//initiate the background task, which should immediately lock the lock and unlock when done
//do some stuff
[lock lock]; //this will pause until the background stuff unlocks
[lock unlock];
[lock release];
Run Code Online (Sandbox Code Playgroud)
Grand Central Dispatch
dispatch_group_t myGroup = dispatch_group_create();
dispatch_group_async(myGroup, dispatch_get_global_queue(), ^{
//stuff to do in the background
});
//do some stuff
dispatch_group_wait(myGroup, DISPATCH_TIME_FOREVER);
dispatch_release(myGroup);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6406 次 |
| 最近记录: |