iOS multithreaded Development (iii)---run Loop (two, III)

Source: Internet
Author: User

Second, when to use the run Loopfor worker threads, run Loop is used only if you need to have more interaction with the thread. For example: 1) Use ports or custom input sources to communicate with other threads2) Using Thread timers3) Use any performselector in cocoa ... Method (refer to Table:performing selectors on other threads)4) Make threads work periodicallyThird, how to use the Run Loop objectThe run Loop object provides an observer for adding input sources, timers and run loops , and an interface to start the run loop, using the run loop to get- ---------------to exit four processes1. Get the object for Run loopA, obtained through Nsrunloop//Get the current thread's run loopnsrunloop *myrunloop = [Nsrunloop currentrunloop];//Cocoa Nsrunloop type conversion process Core Foundation Cfrunloopref typecfrunlooprefç= [Myrunloop getcfrunloop];B, using the Cfrunloopgetcurrent () function2, configure the run LoopThe so-called configuration run loop is primarily to add input sources, timers, or observers to the run loop, which sets the run loop mode. The above function-(void) Observerrunloop is configured with a run loop thread with an observer that adds a timer. Related objects---cfrunloopobserverref objects and Cfrunloopaddobserver functions3, start the run Loopa run loop typically must contain an input source or a timer to listen for events, and if none of them exits immediately after the run loop is started. How to start the Run loop1) Unconditional--The simplest way to start, but the only thing to quit the run loop is to kill it. 2) Set timeout---preset time-out to run running Loop. Run loop runs until an event arrives or the specified time has expired. A, if the event arrives, the message is passed to the appropriate handler to process, and then the run loop exits. You can cycle through the run loop to wait for the next event. B, if the specified time expires, you can use this time to do any other work, then run loop to exit, or directly cycle to restart the run loop. 3) Specific moderunning the run Loop using a specific mode=====running a run Loop:skeleton-(void) Skeletonthreadmain{BOOL done = NO;//Set up a autorelease pool here if not using garbage collection. .........//Add sources/timers to the run loop and does any other setup ......... //The cycle of the run loop Do {//Start the run loop but return after all source is handledSInt32 result = Cfrunloopruninmode (Kcfrunloopdefault, ten, YES); //If a source explicitly stopped the run loop, or if there is no sources or timers, go ahead and exit.if (result = = kcfrunlooprunstopped) | | (Result = = kcfrunlooprunfinished))Done = YES; //Check for all other exit conditions here and set the ' done ' variable as needed .........}while (!done) //Clean up code here. Be sure to release any allocated autorelease pools .........}Note: You can run the run Loop recursively, that is, you can use Cfrunlooprun,cfrunloopruninmode or any Nsrunloop method toinput source or timer handlerStart the Run Loop4, exit run LoopThere are two ways to let run loop exit before processing an eventA, set the time-out for Run loopB, notify run loop stop-----Use the cfrunloopstopped function to explicitly stop the run loop5, thread safety and run loop objectsNsrunloop thread is not securecfrunloop Thread SafetyModify the Run loop object to do this as much as possible inside all threads

iOS multithreaded Development (iii)---run Loop (two, III)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.