Multithreading some understanding

Source: Internet
Author: User

Several implementations of iOS multi-threading

1.pthread. C language but difficult to use, for windows,linux and other platforms, almost no

2.NSThread OC is easy to use, directly manipulating thread objects, but occasionally used.

3.GCD C language is to replace nsthread, such as thread count, make full use of device multicore, automatic management so often used.

4.NSOperation OC Language, based on GCD automatic management, so is also often used.

1. To implement its child threads in Pthread , import <pthread.h>,pthread_t thread;pthread_create ($thread, NULL, function name, NULL), implement the function method, For example void * run (void *param) {For loop a bit, return NULL;}

2.NSThread

The first is to create the thread

-(void) viewdidload {    [super Viewdidload];     Object : nil];    [Thread start];} -(void) Run: (NSString *) param{    for (int i=0; i<10000 ; i++) {        NSLog (@ "%@--%@", Param,[nsthread currentthread].name );    }}

Start thread

There is also a way to create threads

IDID) Waituntildone: (BOOL)]

None of the above is a perfect way.

/*******************************************************/

3.GCD


Full name: Grand Central Dispatch's Hub scheduler

A multi-core parallel solution that automatically leverages the CPU cores. Automate lifecycle management without the need to write any thread management code

Two core concepts: tasks and queues

Two steps: Customizing tasks and adding tasks

  //XOR Ability to open new threads    Dispatch_async (dispatch_queue_t queue, ^{        Code    })    //Same  as    dispatch_sync ( dispatch_queue_t queue, ^{        Code    })

Queue Queues Block task

Two major types of GCD queues

Concurrent queues are valid only at async, and multiple tasks execute concurrently

Serial queue a task is completed before the next task is started

Create a concurrent queue

dispatch_queue_t queue = dispatch_queue_create (constchar *label, dispatch_queue_attr_t attr)

Multithreading Security implications

1. Multiple threads may access the same piece of resources. For example, multiple threads access the same object, the same variable, and a file. Imagine saving and taking money for example. Overwrites the previous data and cannot update the prompt in real time.

2. The solution is to add a lock. Mutual exclusion Lock. Once a thread has finished accessing it, unlock it. @synchronized (lock object Self) but it consumes a lot of CPU resources. Do not recommend using

Nuclear non-atomic properties

Nonatomic

Non-thread-safe, suitable for small memory mobile devices, so all attribute declarations are nonatomic

Try to avoid multiple threads robbing the same resource

Atomic

Atomic attributes are setter methods and consume a lot of resources

Communication between threads

In a process, the thread is not orphaned, and multiple threads need to communicate, such as in the main thread we add UI controls, pictures, etc., download the picture in the child thread, after the download is complete, to display the picture in the main thread

Multithreading some understanding

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.