iOS Learning--Lock

Source: Internet
Author: User

Tag: equals lock generated control get COM write file color process

In the multi-threaded learning, it will inevitably involve the different threads to the same resource, and in the face of this call does not control, it will produce data chaos situation. The most common situation is to buy tickets, multiple ticket sales are bound to operate at the same time, so that will open multiple threads, but this method of ticketing will produce the wrong number of votes.

- (void) Demo1 {//Conductor aNsthread *threada = [[Nsthread alloc] initwithtarget:self selector: @selector (selltikets)Object: nil]; Threada.name=@"Threada";        [Threada start]; //Conductor BNsthread *threadb = [[Nsthread alloc] initwithtarget:self selector: @selector (selltikets)Object: nil]; Threadb.name=@"threadb";    [Threadb start]; }- (void) selltikets{ while(YES) {if(Self.tiketnum >0) {Self.tiketnum-- ; NSLog (@"There are%d tickets left%@", Self.tiketnum,[nsthread CurrentThread]); }Else{NSLog (@"The tickets are sold out%@", [Nsthread CurrentThread]);            Break }              } }

Here for the Nineth ticket sold two times, this is the use of multi-threaded data insecurity problem, because in the multi-threaded resource sharing, there is a lock to ensure that this problem does not occur
- (void) selltikets{ while(YES) {@synchronized (self) {if(Self.tiketnum >0) {Self.tiketnum-- ; NSLog (@"There are%d tickets left%@", Self.tiketnum,[nsthread CurrentThread]); }Else{NSLog (@"The tickets are sold out%@", [Nsthread CurrentThread]);  Break; }            }              }}
@syn need to pass a global variable parameter, not necessarily self, but must not pass a local variable, because the parameters here as a lock, to ensure that the thread execution of the entire process can be locked threads, if it is a local variable, the thread run once was released, there is no point.
The mutex guarantees that the thread is running, that only one thread can access the locked block of code at a time, and the other threads that need access are waiting outside the lock, and when one thread executes, the next thread executes. So, we have to mention another concept in OC, Atomic Atomic. In the declaration of variables, most of us are using nonatomic to improve the efficiency of operation, so what is the role of atomic? In general, when we write a file, we use this code:
    Nsdata*data = [@ "iossynchronized"  datausingencoding:nsutf8stringencoding];    [Data  WriteToFile:@ "ios.text" Atomically:yes];
In order to ensure the security of file writing, we generally choose to use atomicity. In this way, when the text is not directly created when writing a local file to write data, but rather create a virtual file, when the data is written, and then once the local file to be stored, will not appear, write when you delete files, modify the file caused by data conflicts. Similar to a lock, lock the things you want to manipulate, only finish to end. Similarly, declaring atomic to a variable is tantamount to creating a lock on the variable assignment, and only one thread completes the operation of the variable, and the next thread can do it:
@property (Atomic, Strong) NSObject *locklock = _lock; // after re-getter and setter methods, Xcode does not automatically help write member variables -(void) Setlock: (NSObject *)lock  {    @ Synchronized (self) {        lock;    }} -(NSObject *)lock  {    return  _lock;

Locks can ensure data security, but it also brings great inconvenience, that is, the efficiency problem, it will all access operations into a single operation, will waste a lot of time, this is also Apple's Uikit the same thread unsafe way, considering the efficiency problem.

iOS Learning--Lock

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.