Stoping the caller thread is not a good idea imho, if possible, not sure apple like that, but you can get a workaround...
I see to way of doing so :
1/ quick and dirty NSTimer that would check every x second the thread state, not my cup of tea though.
2/ in your doWork selector post a NSNotification when the job is done, and register for it, and when it's fired, you know your thread is done...
I do love the 2nd solution better, and yes, GCD rox so here is how I'd do that:
static NSThread * workerThread;
- (void) startWorkerThread
{
workerThread = [[NSThread alloc] initWithTarget:self selector:@selector(doWork) object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(workerThreadDidReturn) name:@"workerThreadReturnsNotification" object:nil];
[workerThread start];
static NSInteger timeoutSeconds = 5;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeoutSeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// Cancel after timeoutSeconds seconds if not finished yet
if(![workerThread isCancelled]){
[workerThread cancel];
}
});
}
- (void) doWork
{
// Do something heavy...
id result = [NSObject new];
[[NSNotificationCenter defaultCenter] postNotificationName:@"workerThreadReturnsNotification"
object:result];
}
- (void) workerThreadDidReturn:(NSNotification *)notif
{
id result = (id) notify.object;
// do something with result...
[workerThread cancel];
}