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