Objective-c in different ways to achieve lock (ii) -11-multithreading

Source: Internet
Author: User

1objective-C in different ways to implement the lock (ii)2 3In the previous article, we have discussed the use of objective-C Lock several implementations (jump address), but also with the code to actually demonstrate how to build a mutex to achieve multi-threaded resource sharing and thread safety, today we continue to discuss some of the high-level use of locks. 4 5 1. Nsrecursivelock Recursive lock6 7 usually when we use the lock in the code, the most prone to make a mistake is to create a deadlock, and easy to create a deadlock situation is in the recursion or loop, the following code:8 9 1Ten 2 One 3 A 4 - 5 - 6 the 7 - 8 - 9 - Ten +  One -  A +  - A  - at  the -  - -  - -  - -  + -  - in  + -  A to  at +  - -  - the  - *  - $  -Panax Notoginseng  in -  - the //in the main thread +Nslock *thelock =[[Nslock alloc] init]; ATestobj *obj =[[Testobj alloc] init]; the   + //Thread 1 -Dispatch_async (Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^{ $   $     Static void(^testmethod) (int); -TestMethod = ^ (intvalue) -     { the[ThelockLock]; -         if(Value >0)Wuyi         { the [obj method1]; -Sleep5); WuTestMethod (value-1); -         } About [Thelock unlock]; $     }; -   -TestMethod (5); - }); A   + //Thread 2 theDispatch_async (Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^{ -Sleep1); $[ThelockLock]; the [obj method2]; the [Thelock unlock]; the }); the in the above code, is a typical deadlock situation, because the thread 1 in the recursive block, the lock will be multiple times lock, so they are also blocked, because the above code is very short, so it is easy to identify the deadlock, but in the more complex code, it is not so easy to find, So how do you use locks correctly in recursion or in loops? If the thelock is swapped with the Nsrecursivelock object, the problem is resolved, and the lock defined by the Nsrecursivelock class can be locked multiple times on the same thread without causing a deadlock. A recursive lock keeps track of how many times it is lock. Each successful lock must balance the call unlock operation. Only when all locked and unlocked operations are balanced does the lock really get released to other threads.  -  in 2. Nsconditionlock condition Lock the  the When we use multi-threaded, sometimes a lock and unlock locks may not be able to fully meet our use. Because the ordinary lock can only care about the lock and not lock, and do not care about what key to unlock, and we are in the process of resource sharing, most of the situation is only to meet certain conditions to open the lock: About  the 1 the 2 the 3 + 4 - 5 the 6Bayi 7 the 8 the 9 - Ten -  One the  A the  - the  - the  the -  - the  - the  - the  +94  - the //in the main thread theNsconditionlock *thelock =[[Nsconditionlock alloc] init]; the  98 //Thread 1 AboutDispatch_async (Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^{ -      for(intI=0; i<=2; i++)101     {102[ThelockLock];103NSLog (@"thread1:%d", i);104Sleep2); the [Thelock unlockwithcondition:i];106     }107 });108  109 //Thread 2 theDispatch_async (Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^{111[Thelock lockwhencondition:2]; theNSLog (@"thread2");113 [Thelock unlock]; the }); the Thread 1 in the lock using lock, so it is not required conditions, so the smooth lock, but in the unlock used an integral type of condition, it can open the other thread is waiting for the key of the critical ground, and thread 2 needs a key identified as 2, So when thread 1 loops to the last time, it finally turns on blocking in thread 2. But even so, Nsconditionlock also with other locks, is the need to lock and unlock corresponding, just lock,lockwhencondition: and unlock,unlockwithcondition: Can be arbitrarily combined, Of course this is related to your needs.  the 117 3. Nsdistributedlock Distributed lock118 119 all of the above locks are a conflict between multiple threads, but what if there is a need to build mutually exclusive scenarios between several processes or multiple programs? This time we need to use the Nsdistributedlock, from its class name to know that this is a distributed Lock,nsdistributedlock implementation is through the file system, so use it to effectively implement the mutual exclusion between different processes, But Nsdistributedlock does not inherit from Nslock, it does not have a lock method, it only implements Trylock,unlock,breaklock, so if you need lock, you have to implement a trylock polling yourself, Here's a quick demonstration of the code: - 121 Program A:122 123 1124 2 the 3126 4127 5 - 6129 7 the 8131Dispatch_async (Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^{ the     Lock= [[Nsdistributedlock alloc] Initwithpath:@"/users/mac/desktop/earning__"];133[LockBreaklock];134[LockTrylock];135SleepTen);136[Lockunlock];137NSLog (@"Appa:ok");138 });139 Program B: $ 141 1142 2143 3144 4145 5146 6147 7148 8149 9 Max Ten151Dispatch_async (Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^{ the         Lock= [[Nsdistributedlock alloc] Initwithpath:@"/users/mac/desktop/earning__"];153  154          while(! [LockTrylock]) {155NSLog (@"appb:waiting");156Sleep1);157         }158[Lockunlock];159NSLog (@"Appb:ok"); the     });161Run program a first, then run program B immediately, according to the print you can clearly find that when program a just run, program B has been waiting, when about 10 seconds later, program B printed out the output of Appb:ok, the above two different programs to achieve mutual exclusion. /USERS/MAC/DESKTOP/EARNING__ is the address of a file or folder, and if the file or folder does not exist, the file/folder is automatically created when Trylock returns YES. At the end of the file/folder will be cleared, so in the selection of the path, you should choose a non-existent path to prevent accidental deletion of the file.

Objective-c in different ways to achieve lock (ii) -11-multithreading

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.