objective-c--Multithreading Development First day (Pthread/nsthread)

Source: Internet
Author: User
Tags gcd switches

First, why use multithreading?

1, cycle simulation time-consuming task

1. Synchronous execution

2. Asynchronous execution

(Small Secretary for cigarette programming)

3. Process

An application that is running in the system

Each process is independent and runs in its dedicated and protected memory space

Activity Monitor enables you to view the open processes in your Mac system

(Plug-ins to modify the game process of data, plug-in c compilation and other bottom)

4. Threads

1 processes are made up of one or more threads

Is the basic execution unit of a process (at least one thread)

Multithreading:

1 processes can open multiple threads, multiple threads can "simultaneously" perform different tasks

Process-Company, thread-employee: Boss: Main thread

Multithreading can improve the efficiency of program execution

1, Android separated by a virtual machine

2, each transistor in the nucleus is very small, logic does not have the Apple single core performance is high

3, Apple's CPU claims to beat the desktop CPU

Single Core execution principle:

4 threads, thread 1 executes tight wiring 2?

Assumption: Thread 1 dead loop can run? So the hypothesis is not tenable

Multithreading: Do not wait for thread 1 to complete before executing thread 2

The reason that multi-threaded simultaneous, in fact, is to switch, just the time soon near the same

Advantages of Multithreading:

1, can properly improve the execution efficiency of the program

2, can appropriately improve the utilization of resources

Disadvantages of Multithreading: (Cost: Time Cost/space cost

1. Space cost: 512KB of space occupied by each thread

2. Time cost: Create a thread of about 90 mm

3, inter-thread communication, multi-threaded data sharing

Summary: Between threads is CPU switching execution

1, Boss--main thread (UI thread)

Entry of the program: The life cycle of the main (appdelegate) program

All the code is executed through the thread.

The main line Cheng Gan is the UI-related thing.

Performselectioinbackground

Focus: Multi-threaded implementations of iOS:

1, Pthread: A long history, represents a cross-platform approach, a piece of code can be in Iphoneandroid, Windows almost no: Language C

It is difficult to use, and the programmer has to manage its life cycle,

2. Nsthraad (NS Opening Term Foundation framework, OC Language, Object oriented: Encapsulation inheritance dynamic)

or manage the life cycle yourself, occasionally using

3, GCD is also C language written, often used, Apple developed technology, the full use of multi-core, very good optimization, "Transparent"

Automatically Manage threads

4. Nsoperation: (in the foundation frame written by NS OC)

Encapsulation of GCD: the equivalent of inheriting GCD

More simple and useful features than GCD, using more object-oriented

Introduction of Ptread:

P What do you mean: P for POSIX indicates portability (cross-platform)

1 System-Imported header file, with angle brackets

#import <pthread.h>

Pthread_create:

Parameter one, pthread_t: thread marking

Parameter two: Ptheard_attr:attribute: Properties

Null is blank in C language

Parameter three: void * (*) (void *): function signature

void * represents: approximately equal to the ID in OC any one object return value

(*) Letter of function name

(void *) parameter

Create a function to run a function thread:

void * Task (void *param)

{

return NULL;

}

Fill in: Task: First address of function

Parameter four:

void *restrict: A function that gives the third argument a parameter

NULL;

5, Pthread _create return value is a number "0" for success, not 0 fails

if (result = = 0)

NSLog (@ "OK");

Description thread creation was successful

6, [Nsthread CurrentThread]: Print the current thread

As long as number is not equal to 1 is a child thread, Number=1 is a main thread running in

[_bridge NSString] bridging, writing C in OC

Bridging means: The default arc under the OC object has memory management, not C variable management, the role of the bridge is that the C variable is released at the appropriate time

Summarize:

1. import Header File

2. Create Thread Pthread_create

3. Create a function method void *task (Void*param)

4. Passing parameters

Ii. Brief introduction of Nsthread

Nsthread=[nsthread Alloc

Initwithtarget:]

Parameter 1: Object

Parameter 2: Method

Parameter 3: Parameters required by the method

Nsthread don't run when you create it.

This is just to create an object in memory

2. Open Thread

[Thread start]

Method Two, create the thread:

-(void) Demo2

{

Detach: Separation, which can be understood as creating

Detachnewthreadselector:

Parameter 1, method

Parameter 2: Object

Parameter 3: Parameters required by the method

[Nsthread Detachnewthreadselector. ]

}

Method Two determine: If you want to make other setup inconvenience to Nsthread

But the amount of code is low.

Method Three,

Parameter 1: Method

Parameter 2: Parameters of the method

Why there is no parameter: because I use self.

[Self Performselectorinbackground

Never mentioned thread: implicitly creating threads

The life cycle of a thread:

The state of the thread:

1. Creating Threads

2. Open thread: Change in Memory: One more scheduler thread pool: A lot of threads, and the CPU can dispatch it

New (new)-"Ready (Runnable)-" CPU scheduling current Thread-"run (running-" blocking state-"run-" death

Ready: This thread, in addition to the CPU resources, has all the resources in place

The CPU switches back and forth, causing the CPU to switch back and forth between the current thread and the other threads, while the thread switches back and forth between the running and ready states

When running, if the other resources are not in addition to the CPU resources, then call the sleep method \ Wait for the synchronization lock

Because it is not scheduled, it goes into a blocking state and leaves the scheduler thread pool.

If you wake up, will the thread go from blocked state to running state?

No! It will not go directly to the running state, but return to the ready state (sleep is to get the sync Lock)

The thread task executes and dies (death can be suicide: or he can be killed: Force end thread)

Iii. Common Properties of Nsthread

The thread really does this by switching.

Create two threads at a time

Which thread printed the first 0 o'clock?

Not clear: Then give the thread a name

Thread.Name [email protected] ""

Thread.threadpirority

A summary of the basic concepts of Multithreading:

1. Synchronization

2. Asynchronous

Difference: Synchronization is the task order execution, asynchronous is the same time can be multiple run

3. Process: Running applications on the current computer

4. Threads: Basic execution unit of a process, one or more threads in a process

5. Multi-threaded life cycle and common attributes

Features: Thread switching back and forth running

Main thread: Cannot perform time-consuming operations on the main thread

Advantages:

Proper utilization of resources for promotion

Improve the execution efficiency of the program

Disadvantages:

The more threads are not open, the better.

Because: Time cost about 90 milliseconds and space cost about 512KB

Pthread know:

There are three ways to create Nsthread

1, Alloc INITWITHTARGRT

2, Detachnew

3, Performselectorinbackground

The life cycle of a thread:

1. New

2. Ready

3. Operation

4, blocking

5. Death

Common Properties

1. Name

2, Priority Threadpirorty

objective-c--Multithreading Development First day (Pthread/nsthread)

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.