InIPhoneApplication in progressNSThreadCreateRun LoopIt is the content to be introduced in this article, although the iphone provides us with many simple and easy-to-operate thread methods.IPhoneMulti-threaded programming is proposed to use NSOperation and NSOperationQueue, which is really useful. However, in some cases, we still need to use the originalNSThread. This requiresNSThreadCreate a run loop.
- NSThread * thread1 = [[NSThread alloc] initWithTarget: self selector: @ selector (playerThread:) object: nil];
- [Thread start];
- // If you want to use NSOperation, the principle is similar. You just need to add it to the queue .. Queue will call the method at the right time. The following code serves as a reference.
- -(Void) playerThread: (void *) unused
- {
- AudioRunLoop = CFRunLoopGetCurrent (); // The runloop reference of the subthread
- NSAID utoreleasepool * pool = [[NSAID utoreleasepool alloc] init ]; //
- Run loop [self initPlayer]; CFRunLoopRun (); //
- Run loop. This will stop. [Pool release];
- }
- // Implement a timer to check the working status of the sub-thread and perform task switching when appropriate. Or stop yourself when appropriate.
- Run loop-(void) initPlayer {
- // Here you can initialize a work class, such as sound or video playback.
- NSTimer * stateChange = [NSTimer scheduledTimerWithTimeInterval: 0.5 target: self selector:
- @ Selector (checkStatuserInfo: nil repeats: YES];
- }
- -(Void) checkState :( nstmer *) timer
- {
- If (self-thread exited ){
- // Release resources in the Child thread
- CFRunLoopStop (CFRunLoopGetCurrent (); // end the subthread task.
- }
- }
Summary:IPhoneApplication in progressNSThreadCreateRun loopI hope this article will help you!