Re: wait for the event?
Re: wait for the event?
- Subject: Re: wait for the event?
- From: Jerry Krinock <email@hidden>
- Date: Mon, 5 Oct 2009 15:03:14 -0700
You can get the behavior you want by running the run loop, preferably
in a custom mode, but it will require retesting in 10.5 and 10.6 since
much of the run loop stuff has changed. It was suggested to me, and I
now believe, that a more robust and comprehensible approach is to use
an NSConditionLock.
Here is some code that I wrote last week. In my case, I'm putting the
NSConditionLock into a dictionary but you can put it into an ivar or
wherever. Also, in your case, what I call the "manager" thread would
be your main thread.
************** DECLARATIONS
/*!
@brief Creates the receiver's condition lock and sets it into
the receiver's info dictionary.
@details This is one of four methods provided in the event it is
necessary to block a "manager" thread while a "worker" thread
completes work.
This method should be invoked on the "manager" thread just before
spinning off the "worker" thread to do the work.
SSYOperationLinker provides only one such condition lock.
If a condition lock has previously been created by this method
and not yet removed by -blockForLock, an exception will be raised.
*/
- (void)prepareLock ;
/*!
@brief Locks the receiver's condition lock.
@details This is one of four methods provided in the event it is
necessary to block a "manager" thread while a "worker" thread
completes work.
This method should be invoked on the "worker" thread before
beginning the work.
*/
- (void)lockLock ;
/*!
@brief Blocks until the receiver's condition lock is unlocked
by -unlockLock, and then, before returning, removes the condition
lock from the receiver's info dictionary.
@details This is one of four methods provided in the event it is
necessary to block a "manager" thread while a "worker" thread
completes work.
This method should be invoked on the "manager" thread after
spinning off the "worker" thread to do the work.
*/
- (void)blockForLock ;
/*!
@brief Unlocks the receiver's condition lock.
@details This is one of four methods provided in the event it is
necessary to block a "manager" thread while a "worker" thread
completes work.
This method should be invoked on the "worker" thread after work
has been completed.
*/
- (void)unlockLock ;
******* IMPLEMENTATIONS
#define SSY_OPERATION_BLOCKED 0
#define SSY_OPERATION_CLEARED 1
- (void)prepareLock {
NSAssert(([[self info] objectForKey:constKeyLock] == nil), @"Lock
is already in use") ;
NSConditionLock* lock = [[NSConditionLock alloc]
initWithCondition:SSY_OPERATION_BLOCKED] ;
[[self info] setObject:lock
forKey:constKeyLock] ;
[lock release] ;
}
- (void)lockLock {
[[[self info] objectForKey:constKeyLock] lock] ;
}
- (void)blockForLock {
NSConditionLock* lock = [[self info] objectForKey:constKeyLock] ;
BOOL workFinishedInTime = [lock
lockWhenCondition:SSY_OPERATION_CLEARED
beforeDate:
[NSDatedateWithTimeIntervalSinceNow:999999]] ;
if (workFinishedInTime) {
[lock unlock] ;
}
[[self info] removeObjectForKey:constKeyLock] ;
}
- (void)unlockLock {
[[[self info] objectForKey:constKeyLock]
unlockWithCondition:SSY_OPERATION_CLEARED] ;
}
_______________________________________________
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