Pausing an NSThread
Pausing an NSThread
- Subject: Pausing an NSThread
- From: Ron Fleckner <email@hidden>
- Date: Sun, 8 Nov 2009 14:28:47 +1100
Hi all,
I've finally worked out a way to pause a thread and would like to know
if what I'm doing is dangerous or bad or...?
This is a small proof-of-concept/test kind of project. I've had a look
at NSConditionLock, but I don't get the concepts. So I've got this
naïve solution, which is a kind of polling, I know, but it _seems_ to
work quite well. The CPU usage goes down to zero according to
Activity Monitor while the thread is 'paused'. That's gotta be a good
sign.
After detaching a new thread with [NSThread
detachNewThreadSelector:@selector(mySelector:) toTarget:self
withObject:[self arrayOfObjects]];
// New thread starts here
- (void)mySelector:(id)anObject
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLock *myLock = [[NSLock alloc] init];
int loopCounter = 0;
int num = [anObject count]; // it's an NSArray
BOOL isPaused;
while (loopCounter < num)
{
[myLock lock];
isPaused = [self threadPaused];// -threadPaused is a BOOL on
main thread
[myLock unlock];
if (isPaused)
{
while (isPaused)
{
usleep(1000 * 100); // one tenth of a second
[myLock lock];
isPaused = [self threadPaused];
[myLock unlock];
}
}
// Do some stuff with strings from the array and send the
strings to a text view on the main window
loopCounter++;
}
[self
performSelectorOnMainThread:@selector(stopProgAnimation)
withObject:nil waitUntilDone:NO];
[myLock release];
[pool release];
}
Thanks in advance for any insight. My skin is fairly thick, so if you
see something truly awful, please tell me.
Ron
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden