IOS multi-line lock mutex synchronization

Source: Internet
Author: User

 

There are several ways to troubleshoot multi-threaded access to the same memory address in iOS for mutual exclusion synchronization issues:

Method One, @synchronized (ID anobject), (the simplest method)
Automatically locks the parameter objects to ensure code thread safety within the critical area

[CPP]View Plaincopyprint? < param name= "wmode" value= "Transparent" >
    1. @synchronized (self)
    2. {
    3. //This code is mutually exclusive to other @synchronized (self)
    4. //Self points to the same object
    5. }

Method Two, Nslock
The Nslock object implements the Nslocking protocol, which contains several methods:
Lock, plus lock
Unlock, unlocking
Trylock, attempts to lock, if it fails, does not block the thread, but returns no immediately.
Lockbeforedate:, temporarily blocks the thread before the specified date (if no lock is acquired), if the lock is not acquired at maturity, the thread is awakened, and the function immediately returns no
Like what:

[CPP]View Plaincopyprint? < param name= "wmode" value= "Transparent" >
    1. Nslock *thelock = [[Nslock alloc] init];
    2. if ([Thelock lock])
    3. {
    4. //do something here
    5. [Thelock unlock];
    6. }
method Three, Nsrecursivelock, recursive lock
Nsrecursivelock, multiple calls do not block the thread that has acquired the lock.

[CPP]View Plaincopyprint?
  1. Nsrecursivelock *thelock = [[Nsrecursivelock alloc] init];
  2. void myrecursivefunction (int value)
  3. {
  4. [Thelock Lock];
  5. if (value! = 0)
  6. <span style="FONT-SIZE:14PX;" > </span>{
  7. –value;
  8. Myrecursivefunction (value);
  9. }
  10. [Thelock unlock];
  11. }
  12. Myrecursivefunction (5);

Method Four, Nsconditionlock, conditional lock
Nsconditionlock, conditional lock, can set conditions

[CPP]View Plaincopyprint?
  1. Public part
  2. ID condlock = [[Nsconditionlock alloc] initwithcondition:no_data];
  3. //Line Cheng, producer
  4. While (true) {
  5. [Condlock Lockwhencondition:no_data];
  6. //Production data
  7. [Condlock Unlockwithcondition:has_data];
  8. }
  9. //Thread two, consumer
  10. While (true) {
  11. [Condlock Lockwhencondition:has_data];
  12. //Consumption
  13. [Condlock Unlockwithcondition:no_data];
  14. }

Method Five, Nsdistributedlock, distribution lock
Nsdistributedlock, distributed lock, file way implementation, can cross-process
Use the Trylock method to obtain the lock.
Use the Unlock method to release the lock.
If a process that acquires a lock is hung before releasing the lock, the lock will not be released until the lock can be forcibly acquired through Breaklock.

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.