I used to think I was a reasonably intelligent person.
apple's "threading programming guide", has shattered my ego sustaining self deception.
I have a method I want to run repeatedly on a secondary thread, in the example below I've called this doStuff:
I want to be able to repeatedly stop and start the repeated calling of this method.
the code starts the thread.
if the boolean value stuffToDo is true,
then it calls doStuff:
otherwise
it has a little rest.
then it loops again until i tell it to stop
My current code seems wasteful, because it keeps checking 'stuffToDo' even when there is nothing to do.
I could get rid of stuffToDo and just spawn and cancel the thread as needed.
This also seems wasteful, and means I would need to be careful not to accidentally spawn a new thread when I already have one running.
I am sure that the answer to efficiently solving my predicament can be found somewhere in the "run loop management" section of Apple's "threading programming guide"
perhaps it involves custom input sources
But I am really finding this document challenging.
It is as if this document is spawning too many threads in my brain and computation grinds to a halt.
enum eRenderThreadMode
{
render_not_started,
render_run,
render_cancel,
render_finished
};
- (IBAction) startThread:(id)sender
{
self.renderThreadMode = render_run;
label.text = @"doing stuff";
[NSThread detachNewThreadSelector:@selector(keepDoingStuff) toTarget:self withObject:nil];
}
- (void)keepDoingStuff
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
while (renderThreadMode == render_run)
{
if(stuffToDo)
{
[self doStuff];
}
else
{
[NSThread sleepForTimeInterval:kLittleRest];
}
}
self.renderThreadMode = render_finished;
[pool release];
}
- (IBAction)stopThread:(id)sender
{
self.renderThreadMode = render_stop;
while (self.renderThreadMode == render_cancel)
{
[NSThread sleepForTimeInterval:kLittleRest];
}
}