An NSLock object is used to coordinate the operation of multiple threads of execution within the same application. An NSLock object can be used to mediate access to an application’s global data or to protect a critical section of code, allowing it to run atomically.
Questions tagged [nslock]
42 questions
12
votes
2 answers
How to utilize NSLock to prevent a function from firing twice?
I current have a set of asynchronous functions that are both called in the viewDidLoad(). At the end of each function is a bool that is set from false to true upon completion of the function. There is also a conditional statement checking both…

Runeaway3
- 1,439
- 1
- 17
- 43
10
votes
3 answers
Cocoa thread synchronisation when using [ALAssetsLibrary enumerateGroupsWithTypes:]
I have recently, like a few people, discovered that [ALAssetsLibrary enumerateGroupsWithTypes] likes to run its blocks on another thread. What a shame that Apple didn't document that :-)
In my current circumstance I need to wait for the enumeration…

Martin Cowie
- 2,788
- 7
- 38
- 74
8
votes
2 answers
NSLock - should just block when locking a locked lock?
I have a loop which starts with a
[lock lock];
because in the body of the loop I am creating another thread which needs to finish before the loop runs again. (The other thread will unlock it when finished).
However on the second loop I get the…

Nippysaurus
- 20,110
- 21
- 77
- 129
7
votes
1 answer
How to execute a lock that waits until an animation completes?
I am implementing a health bar that animates via user input.
These animations make it go up or down by a certain amount (say 50 units) and are the result of a button press. There are two buttons. Increase and Decrease.
I want to execute a lock on…

Black Orchid
- 111
- 1
- 9
6
votes
1 answer
How to lock an NSLock on a specific thread
I have a property @property NSLock *myLock
And I want to write two methods:
- (void) lock
and
- (void) unlock
These methods lock and unlock myLock respectively and they need to do this regardless of what thread or queue called them. For instance,…

Nosrettap
- 10,940
- 23
- 85
- 140
5
votes
5 answers
ALAssetsLibrary enumerateGroupsWithTypes: - Thread synchronization
I'm using [ALAssetsLibrary enumerateGroupsWithTypes:] to store the ALAssets in an array. As this is an asynchronous operation, I need to wait for it to finish before continuing my work.
I read Cocoa thread synchronisation when using [ALAssetsLibrary…

Leary
- 51
- 1
- 3
4
votes
1 answer
Lock and unlock a semaphore from different threads in Cocoa
I need to use some kind of semaphore to protect the access to a mutex zone, but I need this zone to span multiple threads. This is what I found in the documentation:
Warning: The NSLock class uses POSIX
threads to implement its locking
…

Luca Carlon
- 9,546
- 13
- 59
- 91
4
votes
1 answer
NSLock.lock() executed while lock already held?
I'm reviewing some Alamofire sample Retrier code:
func should(_ manager: SessionManager, retry request: Request, with error: Error, completion: @escaping RequestRetryCompletion) {
lock.lock() ; defer { lock.unlock() }
if let…

Marcus Leon
- 55,199
- 118
- 297
- 429
3
votes
2 answers
IOS SDK - NSLock message: "unlocked from thread which did not lock it"
I´m getting this error using NSLock which I tried to circumvent by using unlockWithCondition (using NSConditionLock), but regardless I get the same result:
* Break on _NSLockError() to debug.
* -[NSLock unlock]: lock ( '(null)') unlocked from…

ZiggyST
- 587
- 6
- 11
3
votes
1 answer
Safe to use NSLock on main thread?
I have a global variable that is accessed from multiple threads, including from the main thread. I’d like to use NSLock because it’s faster than GCD.
Here’s what I’m trying to do:
struct SynchronizedLock {
private var _value: Value
…

TruMan1
- 33,665
- 59
- 184
- 335
3
votes
1 answer
Not using NSLock correctly, but still seems to work
I had issues with thread safety. I have a queue that when I modified the contents caused errors across threads. I hadn't used locks before, but thought I try. I added a lock around all code that manipulated the backing NSMutableArray for my…

Brian
- 3,571
- 7
- 44
- 70
2
votes
2 answers
NSLock "controller" class
I'm writing a multithreaded application for iPhone, and I'm using NSLock's to make sure that some operations (such as loading sounds from file) will behave as atomic. To simplify acquiring locks from different parts of my application, I wrote…

Least
- 23
- 3
2
votes
1 answer
iOS -- trying to setup a dictionary of locks
I have some code that takes an NSString as input and uses that string to create an object. I need to make sure that it does not operate on the same string twice, e.g. if called from different threads, or even if the same thread tries to do it…

William Jockusch
- 26,513
- 49
- 182
- 323
2
votes
2 answers
What is the difference (advantage and disadvantage) between using DispatchGroup and NSRecursiveLock?
What is the difference (advantage and disadvantage) between using DispatchGroup and NSRecursiveLock?
It looks like they are doing exactly the same thing.

Dmitry
- 527
- 5
- 13
2
votes
2 answers
Swift: Understanding NSLock deadlock
Seeing this message in our logs using NSLock:
*** -[NSLock lock]: deadlock ( '(null)')
*** Break on _NSLockError() to debug.
Does this mean that the application has encountered a fatal error and will stop working? Or is…

Marcus Leon
- 55,199
- 118
- 297
- 429