Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8040335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:51:46+00:00 2026-06-05T03:51:46+00:00

I have a question regarding thread safety in Objective-C. I’ve read a couple of

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:51:48+00:00Added an answer on June 5, 2026 at 3:51 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a WCF Service Library and I have a question regarding thread-safety consuming
I have this very straight forward question regarding Thread and Timer classes in Java
I have a question on thread safety while using NSMutableDictionary . The main thread
I have two Years experience in C# programming i have a question regarding thread
I've got a question regarding multiple-thread method invocation in Java. Let's say we have
i have bunch of question regarding to boost thread ? How to initialize the
I have some confusion regarding thread Synchronization . Consider i have two thread Thread1
Some questions regarding the Thread.yield() method. What I have understood is When we call,Thread.Yield(),
I have a question regarding calling methods from different threads. Well I am using
I have question regarding the SQLAlchemy. How can I add into my mapped class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.