I have an nsthread, a while loop within this one. It's getting objects from a "thread-safe" queue in the main method. When I leave the UIViewController, which contains this nsthread object, I call the nsthread cancel method, but it doesn't stop, because it's locked by the "queueLock" NSCondition. When I go back to this UIViewController a new nsthread will be created and gets the objects form the queue, but the previous thread still exits and both of them try to use the same object from the queue, and this causes a memory management problem. My question is how should I stop this thread, when I leave the UIViewController.
NSThread main method:
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
while ([self isCancelled] == NO) {
RenderRequest *renderRequest = [queue get];
[self doRender:renderRequest];
[renderRequest release];
}
[pool drain];
This is the get method of the queue class:
- (id) get {
id toRet = nil;
[queueLock lock];
@try {
while ([queueContents count] == 0) {
[queueLock wait];
}
toRet = [queueContents lastObject];
[queueContents removeLastObject];
}
@finally {
[queueLock unlock];
return toRet;
}
}
Thanks!
I wrote a simple demo, hope this can help you :)
demo.h
demo.m