iOS multithreaded--nsthread

Source: Internet
Author: User

Pthread

Statement pthread_t Cthread;

Create a child thread pthread_create (&cthread, NULL, working, NULL);

1.C method, cross-platform creation of sub-threads, support Unix,linux,macos,ios

2. Need to introduce #import <pthread.h>

3. Advantages: can open a thread, the other almost no

4. Cons: Too C, unfriendly

Nsthread (Child threads)

1.NSThread is a lightweight thread object in Cocoa (Macos,ios)

Simple 2.NSThread operation

(1) Create:

Method one, [Nsthread alloc]initwithtarget~ (Start execution)

Method Two, in order to simplify our operation, NSObject encapsulated method, do not have to alloc to create the automatic execution

Nsthread detachnewthreadselector~

(2) Name:

. Name

(3) Nsthread thread with Alloc must call the Start method to execute

[Thread start];

(4) A thread can be canceled, but it needs to be canceled before it executes.

[Thread start];

[Thread Cancel];

Threads run for a certain amount of time, immediately after the cancellation, when the thread is not running, can be canceled,

In the middle of a sentence with a little longer execution time, the thread is running, it's too late to cancel

(5) Loop Create multiple threads, can set priority

The priority is set between (0~1.0), the higher the value, the higher the priority level

There will be times when it doesn't work, nsthread threads are not too good to control

thread.threadpriority = 0.5;

(6) The exit of the thread, the following method, do not go, the code in the main thread execution, the main thread is also one of the threads, so the main thread will also be forced to quit, this method, must be cautious use

[Nsthread exit]

(7) Calling method (sub-thread update UI)

[Self Performselectoronmainthread: @selector (updataui:) withobject:msg Waituntildone:no];

iOS multithreaded--nsthread

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.