iOS Development-Multithreading Introduction

Source: Internet
Author: User

Multithreading conceptually refers to the implementation of multiple threads from the software or hardware implementation of the technology, the simple point of understanding is that the same time can do multiple things (for example, while listening to the song side code), listen to the song is a thread, code is a thread, if it is a single-core CPU, The above two actions can actually be understood as concurrency, that is, pseudo-parallel, but the difference between the time is too fast unaware. The computer is now generally dual-core or quad-core, more than the previous operation can be improved a lot, iOS six can choose the way to manipulate threads, as follows:

The underlying implementation mechanism for iOS and OS X threads is the Mach thread, and we basically do not process threads to this level, and thread management techniques that can be used in general applications are cocoa Threads,posix threads,mulitprocessing Services, The way we choose combat is cocoa threads.

Apps in iOS and OS X can be single-threaded or multithreaded, and iOS apps are equivalent to a process that is often said on a PC that can extend child threads that have executable stacks and runtime schedules in memory. Threads can communicate with other threads or processes, typically through I/O read-write operations or other ways that you need. A thread in the same application has the same virtual memory space and the right to access the application process, and a simple understanding can be understood as having access to global variables in the method. Many articles on the Internet discuss multi-threading, but rarely say some of the specific thread allocation of space, and I am looking for a picture from Apple's official website about thread space allocation:

Creating the underlying thread is relatively straightforward, but a necessary condition is that there must be a function or method as the main entry point for the thread, so that we can simply instantiate a nsthread thread to demonstrate the effect, Nsthread has two instance methods:

    Nsthread *mythread = [[Nsthread alloc] initwithtarget:self                                                 selector: @selector (methodtask)                                                   Object:nil];        [Nsthread detachnewthreadselector: @selector (methoddetachtask) totarget:self Withobject:nil];

Call Method:

-(void) methodtask{    NSLog (@ "Nstread thread call");} -(void) methoddetachtask{    NSLog (@ "Detach thread call");}

Parameter explanation:

Selector: The method that the thread executes, this selector can only have one parameter, and cannot have the return value.
Target:selector object sent by message
Argument: The only parameter transmitted to target, or nil;

If your current main thread is running and you want to send a message to the main thread, you can performselectoronmainthread Execution:

[Self Performselectoronmainthread: @selector (maintask) Withobject:nil Waituntildone:yes];

Call Method:

-(void) maintask{    NSLog (@ "Performselectoronmainthread thread call");}

There are a lot of threads can write, write a part, we will look at it, write it here today, early to bed and early to good health, everyone goodnight ~

iOS Development-Multithreading Introduction

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.