• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Threads and Locking Question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Threads and Locking Question


  • Subject: Re: Threads and Locking Question
  • From: Dave <email@hidden>
  • Date: Thu, 10 May 2012 22:44:55 +0100

Hi Ken,

Thanks for that I will try it out tomorrow, as an aside, is this ok/ recommended?

-(void) someMethod:(NSString*) theParameter
{
if ([NSThread isMainThread)
	{
[self performSelectorInBackground:@selector(someMethod:) withObject:self];
	return;
	}

//  Do work

}

Thanks again
Dave


On 10 May 2012, at 22:09, Ken Thomases wrote:

On May 10, 2012, at 3:42 PM, Dave wrote:

We are using a third party library that performs tasks asynchronously. This is ok most of the time, but on some occasions I'd like to be able to wait (not on the main thread) until an operation completes,

This is usually an indication of a design problem.


Is there a way of doing this and if so I'd be really grateful if someone could show me the code. I've tried using NSLock and NSConditionalLock etc. but can't get it to work!

NSConditionLock should work. You create it with a condition which you arbitrarily decide means "unsignaled". The block locks it (without regard to condition) and then immediately unlocks it in a different condition which you decide means "signaled". The code that needs to wait just locks it when the condition is signaled, unlocks it, and releases it.

Alternatively, you can use a dispatch_semaphore or dispatch_group for this. Here's an implementation using dispatch_group (written in main, not tested):


-(void) doAtomicOperation
{
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
[self.mLibraryObject doOperation:myOperation withCompletionBlock:^ (void)(ResponseObject* theResponseObject)
		{
			// Do work
			dispatch_group_leave(group);
		}];

//**
//**	Wait for Signal before continuing
//**
		dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
		dispatch_release(group);
}

Regards,
Ken


_______________________________________________

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

  • Follow-Ups:
    • Re: Threads and Locking Question
      • From: Ken Thomases <email@hidden>
References: 
 >[ANN] Online ScriptingBridge documentation archive (From: "email@hidden" <email@hidden>)
 >Threads and Locking Question (From: Dave <email@hidden>)
 >Re: Threads and Locking Question (From: Ken Thomases <email@hidden>)

  • Prev by Date: Re: Threads and Locking Question
  • Next by Date: Re: Threads and Locking Question
  • Previous by thread: Re: Threads and Locking Question
  • Next by thread: Re: Threads and Locking Question
  • Index(es):
    • Date
    • Thread