Summary of iOS multi-thread programming

Source: Internet
Author: User

1. Average thread creation Overhead:

Memory Stack: Master thread -- 1 M, sub thread -- 512 K

Time: Negligible

 

2. multiple ways to create a thread: www.2cto.com

NSThread, POSIX (based on APIs supported by C language), NSObject performSelector

 

3. When the project does not use ARC, the thread executes the body code to create an automatic release pool.

 

4. To ensure thread security, try to avoid using the shared data structure in the thread.

 

5. runloop: An event processing loop in the thread, which is used for non-stop scheduling and processing of input events.

Each thread has its own runloop. The main thread is enabled by default, and the subthread needs to be enabled manually.

Runloop monitors each input source and processes events. If there is no event, runloop sleep without consuming CPU resources

Four situations where runloop needs to be used:


A. Use a port or custom Input Source to communicate with other threads
B. The timer is used in the child thread.
C. Use any javasmselector in cocoa to run the method in the thread.
D. Enable threads to perform periodic tasks
If the NSURLConnection asynchronous request is used in the subthread, runloop is also required. Otherwise, the corresponding delegate method cannot be triggered when the thread exits.


6. Thread Synchronization: a. atomic operation

B. Memory barrier and volatile Variables

Memory barrier: A non-blocking synchronization tool that ensures that memory operations work in the correct order

Volatile: Generally, the compiler optimizes the code by loading the values of these variables into the register. For variables shared between threads, use volatile to force the compiler to read data from the memory every time to ensure data synchronization.

C. Lock: mutex, @ synchronized () command

D. semaphore

 

7. thread security design skills

A. completely avoid data synchronization (less likely)

B. Understand synchronization restrictions

C. Pay attention to the threats to code correctness (ensure data synchronization security and prevent thread deadlocks)

D. use only one lock for Data Synchronization protection to avoid deadlocks.

E. Use the thread synchronization tool,

Atomic operation

Lock: Use POSIX, NSLock (lock, unlock, tryLock), @ synchronized () commands, including implicit exception handling routines to protect code. If an exception occurs, resources are released.

 

8. Thread Security summary:

A. unchangeable objects, usually thread-safe

B. The main thread is responsible for handling response events.

Thread-safe classes and functions: NSArray, NSData, NSNumber .....

Non-thread security: NSBundle, NSCoder, NSArchiver, NSMutableArray

It can only be used for the main thread: nsapplesrept

 

 

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.