Mutex at Obj-c / Cocoa

I am new to multithreaded programming and cocoa.

I will need a lock to access / change iVar NSMutableArray . What is the easiest way to do this?

And while I'm here, do you guys read a bit about multithreaded programming with Obj-c / Cocoa? Thanks.

+4
source share
2 answers

Using the @synchronized block:

 @synchronized(MyArray) { // thread safe code } 

This block is sometimes not recognized by Xcode, but believe me, it works!

For more information, you can read this article.

+10
source

An alternative method is to use GCD and sequential queues.

For your NSArray property, write accessors that use a private serial queue. to set and retrieve values ​​from the backup storage. Since consecutive queues are FIFOs and will run blocks sequentially, this provides thread-safe access to the property.

+1
source

All Articles