Re: Pausing an NSThread
Re: Pausing an NSThread
- Subject: Re: Pausing an NSThread
- From: Dave DeLong <email@hidden>
- Date: Sat, 07 Nov 2009 20:36:03 -0700
Instead of a BOOL on the main thread, what about an NSLock? Start off
by locking it on the main thread, and then the secondary thread can
try to lock it, block (because it can't acquire the lock since the
main thread has it), and not resume until the main thread unlocks it
(equivalent to threadPaused = NO). The only thing is that your thread
method would need to accept both the arrayOfObjects and the lock,
unless you were to make the lock somehow globally available.
With regards to your code, the myLock variable you have is totally
worthless. Locks are used to help coordinate inter-thread
communication, but in your code, only one thread has myLock. So it
locking and unlocking it does nothing except eat cpu cycles.
HTH,
Dave
On Nov 7, 2009, at 8:28 PM, Ron Fleckner wrote:
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
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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