Early experience in multi-thread programming for iPhone

Source: Internet
Author: User
Tags vcard
Early experience in multi-thread programming for iPhone Posted on June 8, 2010 By Aminby

It took one day to complete the multi-thread programming for the iPhone. The class used was nsthread in uikit.
PS: During the Google process, I found many articles with the title similar to <multi-threaded os4 coming>. These ideas are a positive guide. The highlight of 0s4 is multi-task. A task is a process, it is also called multi-process, and multithreading was available on early iPhone OS.
In iPhone OS, the concept of a task is an application. You can only do one thing at a time, that is, you cannot play games at the same time, while playing QQ at the same time.

The process is as follows:
1. Create a thread

    1. [Nsthread detachnewthreadselect: @ selector (beginthread)
    2. Totarget: selft
    3. Withobject: Nil];

2. Do two jobs in the thread. One is dosomethinglongtime in the background processing, and the other is updateui)

View plaincopy to clipboardprint?
    1. -(Void) Beginthread {
    2. [Self defined mselectorinbackgroud: @ selector (dosomethinglongtime)
    3. Withobject: Nil];
    4. [Self perfomselecw.mainthread: @ selector (updateui)
    5. Withobject: Nil
    6. Watuntildone: No];
    7. }

3. How does updateui data come from?

View plaincopy to clipboardprint?
    1. -(Void) Dosomethinglongtime {
    2. // Modify the shared variables varprogress, vartext, and so on.
    3. }
    4. -(Void) Updateui {
    5. // Obtain the shared variables varprogress and vartext, which are displayed on the interface.
    6. }

This completes a general process, but the while (1) cannot be used in updateui. Otherwise, the main thread will be blocked in the updateui function. What should I do? Google has a method, and the updateui method has been modified.

View plaincopy to clipboardprint?
    1. -(Void) Updateui {
    2. // Obtain the shared variables varprogress and vartext, which are displayed on the interface.
    3. If(! Finished)
    4. [Nstimer scheduledtimerwithtimeinterval: 0.2
    5. Target: Self
    6. Selector: @ selector (updateui)
    7. Userinfo: Nil repeats: No];
    8. }

This means that if no thread is finished, the function will return to the function update interface after 0.2 seconds, so the loop ends until the end.

One of the above methods of multi-thread programming for the iPhone, of course, some people suggest using nsoperation and nsoperationqueue, but I tried it and didn't try it out. I will try it again when there is a result.

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.