IOS Recursive lock

Source: Internet
Author: User

Recursive lock

Principle: Recursive locks are also implemented by the Pthread_mutex_lock function, which determines the type of lock within the function. The difference between Nsrecursivelock and Nslock is that the type of the Pthread_mutex_t object inside the package is different, the former is of type pthread_mutex_recursive

Application Scenario: Looping (multiple images loop uploading), recursive

Recursive lock instantiation

Nsrecursivelock *lock = [[Nsrecursivelock alloc] init];

static void (^recursivemethod) (Nsinteger);

//The same line examples multiple locks, will not cause deadlock

Recursivemethod = ^ (Nsinteger value) {

[Lock lock]; //Once in, we'll start locking .

[NetWorkManager requestwithmethod:post url:url parameters:paradic success:^ (id responseobject) {

[Self reuestforsuccess];

Once the data gets successful, it's unlocked, or it causes a deadlock.

[Lock unlock];

} requestrrror:^ (id requestrrror) {

Condition not reached, start loop operation

if (value > 0) {

Recursivemethod (value-1); //Must-1 cycle

}

if (value = = 0) {

condition if = = 0 indicates the number of times the loop condition has been reached can do other things

}

You have to unlock it after the failure.

[Lock unlock];

}];

Remember to unlock

[Lock unlock];

};

Set recursive lock cycle times custom

Recursivemethod (5);

IOS Recursive 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.