Multithread Network in IOS (2) - Inter-thread Communication and Thread Safety

Keywords: network Mobile Attribute iOS

1. Inter-thread communication

In a process, threads often do not exist in isolation, and multiple threads need to communicate frequently.

1.1 Embodiment of Interthreaded Communication
(1) One thread transfers data to another thread;
(2) After a specific task is executed in one thread, it is transferred to another thread to continue the task.

1.2 Common methods of inter-thread communication

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;

- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait;

1.3 Interthreaded Communication Example - Picture Download
Sketch Map:

Code presentation:

-(void)touchesBegan:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
//    [self download2];

    //Open a sub-thread to download the picture
    [NSThread detachNewThreadSelector:@selector(downloadImage) toTarget:self withObject:nil];
}

-(void)downloadImage
{
    //1. Determine the url address of the network image to be downloaded. A url corresponds only to a resource on the network.
    NSURL *url = [NSURL URLWithString:@"http://p6.qhimg.com/t01d2954e2799c461ab.jpg"];

    //2. Download image data to local (binary data) according to url address
    NSData *data = [NSData dataWithContentsOfURL:url];

    //3. Convert the downloaded binary data into pictures
    UIImage *image = [UIImage imageWithData:data];

    //4. Return to the main thread to refresh the UI
    //4.1 First Way
//    [self performSelectorOnMainThread:@selector(showImage:) withObject:image waitUntilDone:YES];

    //4.2 The second way
//    [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];

    //4.3 The third way
    [self.imageView performSelector:@selector(setImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:YES];
}

2. Thread Safety

2.1 Hidden security hazards of multithreading

resource sharing

(1) A resource may be shared by multiple threads, that is, multiple threads may access the same resource;

(2) For example, multiple threads access the same object, the same variable, the same file.

When multiple threads access the same resource, it is easy to cause data confusion and data security problems.

Sketch Map:

2.2 Security Hidden Danger Resolution-Mutual Exclusion Lock

2.2.1 Mutex Use Format

@ synchronized (lock object) {// code to be locked}
Note: Locking a single code only uses one lock, and using multiple locks is invalid.

Advantages and Disadvantages of 2.2.2 Mutex Locks

Advantages: It can effectively prevent data security problems caused by multi-threading robbery of resources;
Disadvantage: It consumes a lot of CPU resources.

Prerequisites for 2.2.3 Mutex Locks

Multiple threads grab the same resource.

2.2.4 Thread Synchronization

(1) Multiple threads execute on the same line (sequentially);

(2) Mutex is the use of thread synchronization technology.

2.3 Atom and Non-Atomicity

OC has two choices when defining attributes: nonatomic and atomic

Atomic: atomic attribute, which locks the setter method (default is atomic);
nonatomic: A non-atomic property that does not lock the setter method.

2.3.1 Selection of Atomic and Non-Atomic Attributes

Comparing nonatomic with atomic:

atomic: thread-safe, consuming a lot of resources
nonatomic: non-thread-safe, suitable for mobile devices with small memory

2.3.2 IOS Development Suggestions

(1) All attributes are declared nonatomic;
(2) Avoid multi-threading grabbing the same resource as far as possible;
(3) As far as possible, the business logic of locking and resource grabbing should be handed over to the server to reduce the pressure of the mobile client.

Posted by squizz on Sun, 07 Apr 2019 09:51:32 -0700