views:

1220

answers:

3

is there Thread in Objective c. if there how it is declare and use.

if anybody know using multithreading in Objective -c ..pls share with me ..

thanks and regards .. by raju..

+3  A: 

If you're developing using Cocoa (ie for the mac or iphone), you have access to the NSThread class, which can be used for multithreading. Googling for NSThread will find you the API.

You can declare it like using:

NSThread *mythread = [[NSThread alloc] initWithTarget:target selector:selector object:argument];

Where target and selector is the object and selector you want to start a thread with, and argument is an argument to send to the selector.

Then use [mythread start] to start it.

Tom
+3  A: 

An easy way to just spin off a method in a new thread is to use.

+ (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument on NSThread. If you aren't running garbage collected you need to set up your own autorelease pool.

Another easy way if you just don't want to block the main thread is to use.

- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg on NSObject

Depending on what type of concurrency you are after you should also take a look at NSOperation that can give you free locking so you can share it between several threads among other things.

monowerker
+2  A: 

You could also look into NSOperation

To see an example of this, have a look at Drew McCormack's post on MacResearch.

Abizern