开发者

Multiple threads in iphone

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

please help 开发者_如何学运维me with sample code.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜