codeBearer codeBearer - 5 months ago 11
Objective-C Question

Locking an object from being accessed by multiple threads - Objective-C

I have a question regarding thread safety in Objective-C. I've read a couple of other answers, some of the Apple documentation, and still have some doubts regarding this, so thought I'd ask my own question.

My question is three fold:

Suppose I have an array,

NSMutableArray *myAwesomeArray;


Fold 1:

Now correct me if I'm mistaken, but from what I understand, using
@synchronized(myAwesomeArray){...}
will prevent two threads from accessing the same block of code. So, basically, if I have something like:

-(void)doSomething {
@synchronized(myAwesomeArray) {
//some read/write operation on myAwesomeArray
}
}


then, if two threads access the same method at the same time, that block of code will be thread safe. I'm guessing I've understood this part properly.

Fold 2:

What do I do if
myAwesomeArray
is being accessed by multiple threads from different methods?
If I have something like:

- (void)readFromArrayAccessedByThreadOne {
//thread 1 reads from myAwesomeArray
}

- (void)writeToArrayAccessedByThreadTwo {
//thread 2 writes to myAwesomeArray
}


Now, both the methods are accessed by two different threads at the same time. How do I ensure that
myAwesomeArray
won't have problems? Do I use something like NSLock or NSRecursiveLock?

Fold 3:

Now, in the above two cases,
myAwesomeArray
was an iVar in memory. What if I have a database file, that I don't always keep in memory. I create a
databaseManagerInstance
whenever I want to perform database operations, and release it once I'm done. Thus, basically, different classes can access the database. Each class creates its own instance of
DatabaseManger
, but basically, they are all using the same, single database file. How do I ensure that data is not corrupted due to race conditions in such a situation?

This will help me clear out some of my fundamentals. Thanks in advance for all your help.

Cheers all!

Answer

Fold 1 Generally your understanding of what @synchronized does is correct. However, technically, it doesn't make any code "thread-safe". It prevents different threads from aquiring the same lock at the same time, however you need to ensure that you always use the same synchronization token when performing critical sections. If you don't do it, you can still find yourself in the situation where two threads perform critical sections at the same time. Check the docs.

Fold 2 Most people would probably advise you to use NSRecursiveLock. If I were you, I'd use GCD. Here is a great document showing how to migrate from thread programming to GCD programming, I think this approach to the problem is a lot better than the one based on NSLock. In a nutshell, you create a serial queue and dispatch your tasks into that queue. This way you ensure that your critical sections are handled serially, so there is only one critical section performed at any given time.

Fold 3 This is the same as Fold 2, only more specific. Data base is a resource, by many means it's the same as the array or any other thing. If you want to see the GCD based approach in database programming context, take a look at fmdb implementation. It does exactly what I described in Fold2.

As a side note to Fold 3, I don't think that instantiating DatabaseManager each time you want to use the database and then releasing it is the correct approach. I think you should create one single database connection and retain it through your application session. This way it's easier to manage it. Again, fmdb is a great example on how this can be achieved.

Edit If don't want to use GCD then yes, you will need to use some kind of locking mechanism, and yes, NSRecursiveLock will prevent deadlocks if you use recursion in your methods, so it's a good choice (it is used by @synchronized). However, there may be one catch. If it's possible that many threads will wait for the same resource and the order in which they get access is relevant, then NSRecursiveLock is not enough. You may still manage this situation with NSCondition, but trust me, you will save a lot of time using GCD in this case. If the order of the threads is not relevant, you are safe with locks.

Comments