title: Is Objective-C's NSMutableArray thread-safe?
body: <p>I've been trying to fix this crash for almost a week. The application crashes without any exception or stack-trace. The application does not crash in any way while running through instruments in zombie mode.</p>
  
  <p>I have a method that gets called on a different thread.
  The solution that fixed the crash was replacing</p>
  
  <pre><code>[self.mutableArray removeAllObjects];
  </code></pre>
  
  <p>with </p>
  
  <pre><code>dispatch_async(dispatch_get_main_queue(), ^{
      [self.searchResult removeAllObjects];
  });
  </code></pre>
  
  <p>I thought it might be a timing issue, so I tried to synchronize it, but it still crashed:</p>
  
  <pre><code>@synchronized(self)
  {
      [self.searchResult removeAllObjects];
  }
  </code></pre>
  
  <p>Here is the code</p>
  
  <pre><code>- (void)populateItems
  {
     // Cancel if already exists  
     [self.searchThread cancel];
  
     self.searchThread = [[NSThread alloc] initWithTarget:self
                                                 selector:@selector(populateItemsinBackground)
                                                   object:nil];
  
      [self.searchThread start];
  }
  
  
  - (void)populateItemsinBackground
  {
      @autoreleasepool
      {
          if ([[NSThread currentThread] isCancelled])
              [NSThread exit];
  
          [self.mutableArray removeAllObjects];
  
          // Populate data here into mutable array
  
          for (loop here)
          {
              if ([[NSThread currentThread] isCancelled])
                  [NSThread exit];
  
              // Add items to mutableArray
          }
      }
  }
  </code></pre>
  
  <p>Is this problem with NSMutableArray not being thread-safe?</p>