Multithreading thread Synchronization issues

Source: Internet
Author: User

//  DYFViewController.m //  623-05-线程同步问题 // //  Created by dyf on 14-6-23. //  Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved. // #import "DYFViewController.h" @interface DYFViewController () @property ( nonatomic , assign) int leftCount; @property ( nonatomic , strong) NSThread *thread0; @property ( nonatomic , strong) NSThread *thread1; @property ( nonatomic , strong) NSThread *thread2; @end @implementation DYFViewController - ( void )viewDidLoad {      [ super viewDidLoad];      // Do any additional setup after loading the view, typically from a nib.           // 默认有100张      self .leftCount = 100;      // 开启多条线程同时卖票      self .thread0 = [[ NSThread alloc] initWithTarget: self selector: @selector (saleTicket) object:@ "000" ];      self .thread0.name = @ "0000" ;      // 优先级      self .thread0.threadPriority = 1.0;           self .thread1 = [[ NSThread alloc] initWithTarget: self selector: @selector (saleTicket) object:@ "000" ];      self .thread1.name = @ "1111" ;      self .thread1.threadPriority = 0.0;           self .thread2 = [[ NSThread alloc] initWithTarget: self selector: @selector (saleTicket) object:@ "000" ];      self .thread2.name = @ "2222" ;      self .thread2.threadPriority = 0.0;           } /**   *  卖票   */ - ( void )saleTicket { //    NSLock *lock = [[NSLock alloc] init]; //    lock.tryLock //    lock.lock           while (1) {               @synchronized ( self ){ // 开始枷锁                   // 1.先检查票数          int count = [ self leftCount];          if (count > 0) {              // 票数 - 1              [ self setLeftCount:(count - 1)];              // 暂停              [ NSThread sleepForTimeInterval:0.0004];              NSThread *currentT = [ NSThread currentThread];              NSLog (@ "%@,-------%d" , currentT.name, self .leftCount);          } else          {              // 退出线程              [ NSThread exit];          }          } // 解锁      } } - ( void )touchesBegan:( NSSet *)touches withEvent:(UIEvent *)event {      [ self .thread0 start];      [ self .thread1 start];      [ self .thread2 start]; } @end

Summary:

--------------Thread Security issues (multithreading security implications)-------

1. Resource Sharing

• A resource may be shared by multiple threads, that is, multiple threads may access the same piece of resources

• For example, multiple threads access the same object, the same variable, the same file

2. When multiple threads access the same resource, data confusion and data security issues can easily arise

3. Example

eg 1 Save money

eg 2 Selling tickets

-----------------security vulnerability Resolution-Mutex lock-----

1. Format

@synchronized (Lock Object)

{//code that needs to be locked

}

Note: Locking a copy of the code can only be used with 1 locks, which is not valid with multiple locks

2. Pros and cons

• Advantages: Can effectively prevent multi-threaded snatch resources caused by security problems

• Cons: Consumes a lot of CPU resources

3. Use of mutexes: Multiple threads rob the same piece of resources

4. Related terms: Thread synchronization

means: Multiple threads perform tasks sequentially

• Mutex is the use of thread synchronization technology

Multithreaded thread synchronization issues

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.