Problem cancelling lengthy task in secondary thread
Problem cancelling lengthy task in secondary thread
- Subject: Problem cancelling lengthy task in secondary thread
- From: Jörn Salewski <email@hidden>
- Date: Thu, 30 Oct 2003 12:53:33 +0100
Maybe someone can help.
I'm implementing an application to do fulltext search of literary works
contained on a CD. I've set up a secondary search-worker thread to do the
actual work using DO.
The worker object contains a boolean flag that will be changed when the user
cancels the search in the main/ gui thread and that should be checked during
the search loop. The flag is declared volatile.
The search routine consist of three nested loops.
Now, my problem is, that as long as the innermost loops are busy the flag is
not changed, only if they are done and it's up to the outer loop to get the
next work of an author, the flag is properly changed and the task is
cancelled.
If I change the code to check not this boolean value, but to check for a
similar return value in the main thread, the search can be cancelled
imediately, but this seams to be to much DO messaging overhead.
Has anybody got an idea of what's going wrong?
Best regards,
JS
Here is some of my (pseudo-)code to give a better picture:
- (oneway void) setContinueSearch: (BOOL)flag {
_continue = flag;
}
- (oneway void) searchIn:(id)_TOCAuthor {
unsigned numberOfWorks = [_TOCAuthor numberOfWorks];
unsigned momentaryWork = 0;
// going through all _TOCWorks in _TOCAuthor
for (; momentaryWork < numberOfWorks; momentaryWork++) {
// here it works
if (! _continue) break;
id _TOCWork = [_TOCAuthor workAtIndex:momentaryWork];
const char *text = [[_TOCWork completeText] UTF8String];
int error = 0;
if ((error = regexec(&preg, text, 1, result, 0)) == 0) {
//if (! _continue) goto CANCEL;
if (![_controller searchInProgress]) {
goto CANCEL;
}
// process the result
text += result[0].rm_eo;
while ((error = regexec(&preg, text, 1, result, 0)) == 0) {
//if (! _continue) goto CANCEL;
if (![_controller searchInProgress]) {
goto CANCEL;
}
// process the result
text += result[0].rm_eo;
}
}
// error
if (error > 1) {
// handle regex error
}
} // END going through all _TOCWorks in _TOCAuthor
CANCEL:
[_controller finishedSearch...];
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.