Multi-thread programming for iOS: three levels of thread applications

Source: Internet
Author: User

IOS supports three levels of thread programming, from the underlying layer to the high-level (the higher the level, the more convenient to use, the more concise) are:

1: thread;

2: cocoa operations;

3: Grand Central Dispatch;

Introduction:

ThreadIt has the lowest abstraction level. The other two thread applications encapsulate thread. For programmers, thread is relatively troublesome and requires programmers to manage thread cycles, but the efficiency is the highest. There are two types of threads: Cocoa
Threads -- use nsthread or use the nsobject class method javasmselectorinbackground: withobject: to create a thread; POSIX Threads: a multi-thread Library Based on the C language.

There are three ways to create nsthread:

I. [nsthread detachnewthreadselector: @ selector (mythreadmethod :) totarget: Self withobject: Nil]; call to create a new thread to execute the operation immediately
Ii. nsthread * mythread = [[nsthread alloc] initwithtarget: Self selector: @ selector (mythreadmethod :)
Object: Nil]; [mythread start]; after nsthread initialization, the new thread does not execute, but calls
The thread will be created for execution only when start is enabled. This method is more flexible than the above method. Before starting a new thread, you can perform operations on the thread, such as setting the priority and locking.

3: [myobj implements mselectorinbackground: @ selector (mythreadmainmethod)
Withobject: Nil]; Use the nsobject class method to create a thread:

All of the above can be called in the new thread:
Withobject: waituntildone: update the UI because the sub-thread cannot directly update the UI.
Thread Synchronization and lock:

In many cases, multiple threads access the same data. To avoid conflicts between thread a and thread B and execution sequence, you need to use nscondition and nslock, ensure thread (atomic operation) Security.

The sequence of synchronous execution with nscodition, nscodition
It is a special type of lock, which can be used to synchronize the sequence of Operation execution. The difference between it and mutex is that it is more accurate. Wait until a nscondtion thread is locked until other threads send a signal to that condition. The following is an example:

-(Void) applicationdidfinishlaunching :( uiapplication *) Application {
Tickets = 100;
Count = 0;

Ticketcondition = [[nscondition alloc] init]; // Lock Object
Ticketsthreadone = [[nsthread alloc] initwithtarget: Self selector: @ selector (run) object: Nil];
[Ticketsthreadone setname: @ "thread-1"];
[Ticketsthreadone start];

TicketsThreadtwo = [[NSThread alloc] initWithTarget: self selector: @ selector (run) object: nil];
[TicketsThreadtwo setName: @ "Thread-2"];
[TicketsThreadtwo start];
[Window makeKeyAndVisible];
}

-(Void) run {
While (TRUE ){
[TicketsCondition lock]; //
Lock
If (tickets> 0 ){
[NSThread sleepForTimeInterval: 0.5];
Count = 100-tickets;
NSLog (@ "Current ticket count: % d, sold: % d, thread name: % @", tickets, count, [[NSThread currentThread] name]);
Tickets --;
} Else {
Break;
}
[TicketsCondition unlock];
}
}

Inter-thread communication and interaction
Do things in the main thread of the application:
Performselectoronmainthread: withobject: waituntildone:
Performselecstmmainthread: withobject: waituntildone: modes:
Do things in the specified Thread:
Performselector: onthread: withobject: waituntildone:
Performselector: onthread: withobject: waituntildone: modes:
Do things in the current thread:
Optional mselector: withobject: afterdelay:
Optional mselector: withobject: afterdelay: inmodes:
Cancels a message sent to the current thread.
Cancelpreviousperformrequestswithtarget:
Cancelpreviousperformrequestswithtarget: selector: object:

Cocoa Operetions

Related Article

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.