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 599767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:32:02+00:00 2026-05-13T16:32:02+00:00

I have an app (using retain/release, not GC) that maintains an NSArray instance variable,

  • 0

I have an app (using retain/release, not GC) that maintains an NSArray instance variable, which is exposed as a property like so:

@interface MyObject : NSObject
{
    NSArray* myArray;
}
@property (copy) NSArray* myArray;
@end

I want to access the contents of this array from a secondary thread, which is detached using -performSelectorInBackground:withObject:. It is possible and indeed likely that the array will change during the execution of the secondary thread.

In the secondary thread I want to do something like this:

if([self.myArray containsObject:foo])
{
    //do stuff
}

From reading the threading documentation, it seems I should be able use the @synchronized directive in the accessors like so:

@implementation MyObject
- (NSArray *)myArray
{
    NSArray *result;
    @synchronized(self)
    {
        result = [myArray retain];
    }
    return [result autorelease];
}

- (void)setMyArray:(NSArray *)aMyArray
{
    @synchronized(self)
    {
        [myArray release];
        myArray = [aMyArray copy];
    }
}
@end

Is this all I need to do to ensure thread safety, or is it more complex?

Update: I’ve subsequently found a great article on Apple’s site that addresses this issue in depth: http://developer.apple.com/mac/library/technotes/tn2002/tn2059.html

  • 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-05-13T16:32:02+00:00Added an answer on May 13, 2026 at 4:32 pm

    Your code above protects you from setting the array concurrently, or getting the array while another is setting it. Since it is a non-mutable array, this protects the array itself just fine.

    However, if by “the array will change” you mean you’ll be editing the items inside the array, you could still have some problems. For example, if the array was filled with NSMutableStrings, and you had a thread that ran:

    NSMutableString *foo = [myObject.myArray objectAtIndex:0];
    [foo appendString:@"foo"];
    

    and another that ran

    NSMutableString *bar = [myObject.myArray objectAtIndex:0];
    [bar appendString:@"bar"];
    

    The access to the array would be safe (one thread would have to wait for the other to access it), however, access to the foo/bar pointer (which is the same) would not be, since both calls to ‘appendString’ are outside of the @synchronized block.

    If this is how your array will change, you’ll need to synchronize these points of access as well. Either with more @synchronized blocks, or other types of locks. See Using Locks

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

Sidebar

Ask A Question

Stats

  • Questions 448k
  • Answers 449k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I would create a protocol for the level 2 controller… May 15, 2026 at 8:03 pm
  • Editorial Team
    Editorial Team added an answer Strings are immutable, so you have to make a char[]… May 15, 2026 at 8:03 pm
  • Editorial Team
    Editorial Team added an answer Just in case you want to understand how it works… May 15, 2026 at 8:03 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.