IosGCD thread-safe Resource Sharing

Source: Internet
Author: User

IosGCD thread-safe Resource Sharing

The magic way is to lock. Let you get through one by one. Don't worry, wait in the queue and buy steamed bread. Alas, who told you not to worry? Look at you.

 

//

// ViewController. m

// Thread security

//

// Created by wangqian on 15/4/11.

// Copyright (c) 2015 wangqian. All rights reserved.

//

 

# Import "ViewController. h"

 

@ Interface ViewController ()

 

 

@ Property (nonatomic, assign) NSUInteger ticketNumber;

 

 

@ Property (nonatomic, retain) NSThread * thread1;

 

@ Property (nonatomic, retain) NSThread * thread2;

 

 

@ Property (nonatomic, retain) NSThread * thread3;

 

 

// Lock

@ Property (nonatomic, retain) NSLock * lock;

 

 

@ End

 

@ Implementation ViewController

 

-(Void) viewDidLoad {

[Super viewDidLoad];

 

// Thread security

// In multithreading, multiple threads often need to access or modify the same resource. If not processed, problems may occur when multiple threads access or modify the resource.

 

// A resource can only be operated by one thread within a time period.

 

// Sell steamed buns, with multiple threads selling steamed buns at the same time

 

// Set the initial steamed bread

Self. ticketNumber = 50;

 

 

// Create a lock resource and create a lock

Self. lock = [[NSLock alloc] init];

 

 

Self. thread1 = [[NSThread alloc] initWithTarget: self selector: @ selector (sellTickets :) object: @ "thread a sells steamed buns"];

 

[Self. thread1 setName: @ "attendant a"];

 

 

Self. thread2 = [[NSThread alloc] initWithTarget: self selector: @ selector (sellTickets :) object: @ "thread B sells steamed buns"];

[Self. thread2 setName: @ "attendant B"];

 

 

 

Self. thread3 = [[NSThread alloc] initWithTarget: self selector: @ selector (sellTickets :) object: @ "thread c sells steamed buns"];

 

 

[Self. thread3 setName: @ "Waiter c"];

 

 

}

 

 

-(Void) sellTickets :( id) obj

{

// When multiple threads access or modify the same resource, the resource should be protected (locked)

// Lock note: the area affected by the lock should be small enough.

// Critical section: Use code area of shared resources.

// Lock advantages: protection of shared resources

// Disadvantage: a large amount of CPU resources is consumed.

While (true ){

 

// It's more convenient than the second method. It's amazing and simple. You just need to do it in one sentence.

// Add a mutex lock to ensure that only one thread can use shared resources for a period of time

@ Synchronized (self)

{

If (self. ticketNumber> 0) {// sell steamed buns when the number is greater than 0

Self. ticketNumber-= 1;

NSThread * thread = [NSThread currentThread];

NSLog (@ "obj = % @, % @ sold a steamed bread, with % ld steamed bread left", obj, [thread name], self. ticketNumber );

} Else

{

// Exit the current thread

[NSThread exit];

}

}

}

}

 

 

-(Void) sellTickets2 :( id) obj

{

 

 

While (true ){

[Self. lock]; // lock

If (self. ticketNumber> 0) {// if the number of votes is greater than 0, the ticket is sold.

 

// Decrease the number of votes by 1

Self. ticketNumber-= 1;

 

 

NSThread * thread = [NSThread currentThread];

 

 

NSLog (@ "obj = % @, % @ sold a steamed bread, with % ld steamed bread left", obj, [thread name], self. ticketNumber );

 

} Else

{

 

 

// Exit the current thread

[NSThread exit];

 

}

[Self. lock unlock]; // unlock

 

 

 

}

 

 

}

 

 

// The user clicks to start selling steamed buns

-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event

{

 

 

[Self. thread1 start];

 

[Self. thread2 start];

 

 

[Self. thread3 start];

 

}

 

 

@ End


This is the third day of the day. Good night.

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.