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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:26:52+00:00 2026-06-09T14:26:52+00:00

If I have two different threads via GCD accessing an NSMutableArray and one is

  • 0

If I have two different threads via GCD accessing an NSMutableArray and one is merely creating a new array based off the mutable array while the other thread is deleting records from the array, should I expect this to be a problem? That is, shouldn’t the copy, which I presume is merely “reading” the array, just get whatever happens to be in the array at that moment? I am not enumerating the array in either thread, but it is still crashing. As soon as I remove the read routine, it works fine.

Here is the “read” :

  dispatch_async(saveQueue, ^{

    NSDictionary*tempstocks=[NSDictionary dictionaryWithDictionary:self.data];

It crashes on this thread with: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[9]'

Here is what is happening on another thread:

[self.data removeObjectForKey:item];

I know you cannot mutate while enumerating, but I’d think it would be okay to read while mutating, you might not know which version of the mutated object you get, but I wouldn’t think this is a problem, but clearly it is. Perhaps the dictionaryWithDictionary method is performing an operation that first sees X objects but by the time the routine is done it contains X-Y objects, thus it is not “capturing” the entire self.data dictionary in one snap when it runs dictionaryWithDictionary and is instead enumerating over self.data which would essentially be the same problem as mutation while enumeration?

  • 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-09T14:26:54+00:00Added an answer on June 9, 2026 at 2:26 pm

    I guess that you might create three different queues using GCD: one for save, second one for something else and last one to operate with NSMutableArray.

    dispatch_async(saveQueue, ^{
        dispatch_barrier_async(_queue, ^{
                NSDictionary*tempstocks=[NSDictionary dictionaryWithDictionary:self.data];
            });
    });
    
    dispatch_async(anotherQueue, ^{
        dispatch_barrier_async(_queue, ^{
                [self.data removeObjectForKey:item];
            });
    });
    

    It’s like @synchronize but using GCD.

    More info: GCD Reference/dispatch_barrier_async and http://www.mikeash.com/pyblog/friday-qa-2011-10-14-whats-new-in-gcd.html

    EDIT

    I have made a couple of performance test in order to understand which of the way is faster:

    - (void)usingSynchronized
    {
        dispatch_queue_t writeQyeue = dispatch_queue_create("com.tikhop.writeQyeue", DISPATCH_QUEUE_CONCURRENT);
        dispatch_sync(writeQyeue, ^{
            for(size_t i=0; i<10000; i++)
                @synchronized (arr) {
                    [arr replaceObjectAtIndex:0 withObject:[NSNumber numberWithInt:1]];
                    [arr replaceObjectAtIndex:0 withObject:[NSNumber numberWithInt:2]];
                    [arr replaceObjectAtIndex:0 withObject:[NSNumber numberWithInt:3]];
                    [arr replaceObjectAtIndex:0 withObject:[NSNumber numberWithInt:4]];
                }
        });
    }
    
    - (void)usingGCD
    {
        dispatch_queue_t writeQyeue = dispatch_queue_create("com.tikhop.writeQyeue", DISPATCH_QUEUE_CONCURRENT);
        dispatch_sync(writeQyeue, ^{
            for(size_t i=0; i<10000; i++)
                dispatch_barrier_async(_queue, ^{
                    [arr replaceObjectAtIndex:0 withObject:[NSNumber numberWithInt:5]];
                    [arr replaceObjectAtIndex:0 withObject:[NSNumber numberWithInt:6]];
                    [arr replaceObjectAtIndex:0 withObject:[NSNumber numberWithInt:7]];
                    [arr replaceObjectAtIndex:0 withObject:[NSNumber numberWithInt:8]];
                });
        });
    }
    
    arr = [NSMutableArray arrayWithCapacity:1];
    [arr addObject:@(0)];
    
    [self usingSynchronized];
    [self usingGCD];
    

    I got the following result:
    enter image description here

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

Sidebar

Related Questions

I have two different threads. the first one is the main thread that must
I have two different libGL libraries on the same Ubuntu 11.04 machine. One library
I have two different arrays, one with strings and another with ints. I want
I have two different modules that need access to a single file (One will
I have two threads and one class. Thread1 updates the local object of the
I have an object that is called from two different threads and after it
I have a network of Java Threads (Flow-Based Programming) communicating via fixed-capacity channels -
Under what circumstances is it unsafe to have two different threads simultaneously writing to
I have two functions that I want to run on different threads (because they're
I have a one String which will be access by four different threads in

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.