tags:

views:

36

answers:

2

how to create multyple threads in iphone(in single class)

please help me with sample code.

A: 

Check out the documentation for NSThread, NSOperationQueue, and the various performSelector... methods defined for NSObject.

Kristopher Johnson
thank u for ur answer this may help me...
sandeep
A: 

NSThread Class reference

An example to run a child thread:

- (void)threadRun
{
  // One thread, one autorelease pool. as main thread's pool inside main.c
  NSAutoreleasePool *aPool = [[NSAuroreleasePool alloc] init];

  // do-job in thread here

  [aPool release];
}

- (void)performJobWithThread
{
    // Method 1:
    NSThread *aThread = [[NSThread alloc] initWithTarget:self 
                                          selector:@selector(threadRun)
                                          object:nil];

    [aThread start];

    /// Release aThread at some point avoid memory leak.

    // Method 2:
    [NSThread detachNewThreadSelector:@selector:@selector(threadRun)
                             toTarget:self
                             withObject:nil];
}

Before using NSThread, it's better to read Threading Programming Guide first. It will tell you about memory management, communication with other thread, ..., etc.

NSOperation and NSOperationQueue are good to design multithread. But I am learning them now and I am unable to talk about them clearly.

Toro