Objective c 如何在不使用委托/回调的情况下连接Objective C中的线程?
在Objective C中有没有一种干净的连接线程的方法,就像Java中的“Thread.join”一样?我找到了performSelector:onThread:withObject:waitUntilDone:的方法,但它的局限性是我不能在另一行调用“阻塞”,因为我想这样做:Objective c 如何在不使用委托/回调的情况下连接Objective C中的线程?,objective-c,multithreading,join,Objective C,Multithreading,Join,在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]; 提前
[dispatch Thread A];
[process something on main thread];
[wait for Thread A to finish before proceeding];
提前谢谢。您可以使用signal/wait。我不知道有任何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];
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];
NSLock
NSThread * otherThread = [[NSThread alloc] initWithTarget:self selector:@selector(methodToPerformInBackground:) object:aParameter];
[otherThread start];
//do some stuff
while ([otherThread isFinished] == NO) {
usleep(1000);
}
[otherThread release];
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];
中央大调度
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);
你能用锁来锁吗?换句话说,类似这样的东西(伪代码)
NSConditionLock是我问题的答案,抱歉Dave DeLong,但我不能使用:
#define T_START 0
#define T_FINISHED 1
-(void) updateVerticalScopeBackground: (id) aParam {
[lockForThread lock];
NSAutoreleasePool *pool = [NSAutoreleasePool new];
//do something
[pool release];
[lockForThread unlockWithCondition:T_FINISHED];
}
-(void) sumFunc {
lockForThread = [[NSConditionLock alloc]
initWithCondition: T_START];
NSThread* updateVerticalScope = [[NSThread alloc] initWithTarget:self selector:@selector(updateVerticalScopeBackground:) object:nil];
[updateVerticalScope start];
//do some processing
[lockForThread lockWhenCondition:T_FINISHED];
[lockForThread unlockWithCondition:T_FINISHED];
[lockForThread release];
}
您永远不希望主线程在等待另一个线程时被阻塞。至少你不会在任何有用户界面的应用程序中使用,因为如果主线程被阻塞,你的应用程序就会被冻结
如果主线程启动后台线程,完成其他需要做的事情,然后返回到运行循环,那就更好了。后台线程将通过发送
-performSelectorOnMainThread:waitUntilDone:
警告:NSLock类使用POSIX线程来实现其锁定行为。向NSLock对象发送解锁消息时,必须确保该消息是从发送初始锁定消息的同一线程发送的。从不同线程解锁锁可能会导致未定义的行为。“参考。这意味着我无法从调度的线程锁定/解锁NSLock?抱歉,没关系,我误解了消息。初始锁定和解锁应该在同一个线程上,在这种情况下,您的示例非常好。再次感谢。我认为您必须更改这一行:[lockForThread unlockWithCondition:T_FINISHED]代码>,使用此代码:[lockForThread unlock]代码>。你怎么认为?