Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSMutableArray is thread safe?

Can anybody give me example that NSMutableArray is thread safe or not?

like image 514
RAMAN RANA Avatar asked Feb 16 '11 13:02

RAMAN RANA


People also ask

What is NSMutableArray?

The NSMutableArray class declares the programmatic interface to objects that manage a modifiable array of objects. This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray . NSMutableArray is “toll-free bridged” with its Core Foundation counterpart, CFMutableArray .

What is thread-safe in iOS?

Thread Safety APIs in iOSBy creating a queue of tasks where only one task can be processed at any given time, you are indirectly introducing thread safety to the component that is using the queue.


2 Answers

It is not thread safe. See the list of thread safe/unsafe classes here

like image 94
Caner Avatar answered Oct 22 '22 13:10

Caner


According to the Apple docs NSMutableArray is not thread safe.

Mutable objects are generally not thread-safe. To use mutable objects in a threaded application, the application must synchronize access to them using locks. (For more information, see “Atomic Operations”). In general, the collection classes (for example, NSMutableArray, NSMutableDictionary) are not thread-safe when mutations are concerned. That is, if one or more threads are changing the same array, problems can occur. You must lock around spots where reads and writes occur to assure thread safety.

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html

like image 39
SamG Avatar answered Oct 22 '22 13:10

SamG