Nslock Simple Application

Source: Internet
Author: User

1 #import "AppDelegate.h"2 3 @interfaceappdelegate ()4 5 @end6 7 @implementationappdelegate8 9-(void) dealloc{Ten      One [_window release]; A [Super Dealloc]; - } -  the  --(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions { -Self.window =[[UIWindow alloc]initwithframe:[uiscreen mainscreen].bounds]; -Self.window.backgroundColor =[Uicolor Blackcolor]; +      - [Self.window makekeyandvisible]; +      A [_window release]; at      -     //thread mutexes do not have their own independent stack space, all using the internal memory space of the process, so it is possible that multiple threads access the same piece of memory at the same time, there will be problems, access to the shared resources for multi-threaded is a thread mutex, add the line lock -Nslock *lk =[[Nslock alloc]init]; -      -Nsthread *thread1 = [[Nsthread alloc]initwithtarget:self selector: @selector (Therad:)Object: LK]; -      inThread1.name =@"Window No. 1th"; -      to [thread1 start]; +      -      the      *Nsthread *therad2 = [[Nsthread alloc]initwithtarget:self selector: @selector (Therad:)Object: LK]; $     Panax NotoginsengTherad2.name =@"window No. 2nd" ; -      the //therad2.threadpriority = 0.9; +      A [therad2 start]; the      +      -      $Nsthread *therad3 = [[Nsthread alloc]initwithtarget:self selector: @selector (Therad:)Object: LK]; $      -Therad3.name =@"Window No. 3rd" ; -      theTherad3.threadpriority =1 ; -     Wuyi [therad3 start]; the      -      Wu      -      About      $      -     returnYES; - } -  A  + //Mock Ticketing the-(void) Therad: (ID)Object{ -      $Nslock *lk = (Nslock *)Object ; the      the     //number of votes 100 sheets the     Static intNumber = - ; the      -      while(1) { in          the         //Add line lock to improve security of data access the[LKLock]; About          theNumber-- ; the          theNSLog (@"%@%d", [[Nsthread currentthread]name],number]; +          -           //Sleep 1 seconds theSleep1);Bayi          the         if(Number = =0) { the              Break ; -         } -          the [lk unlock]; the     } the      the}

Nslock Simple Application

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.