Alexander
Alexander

Reputation: 233

Property/fields thread synchronization

I have read some stuff on ios sdk about multithreading, but I still didn't find the answer to the problem: In the main thread I have a property, the program does some stuff in a thread, where the value of the property is changed, the other thread needs that changed value. So how can I change the value of a property or a field in one thread, so that it would change for all threads?

Upvotes: 0

Views: 739

Answers (1)

MechEthan
MechEthan

Reputation: 5703

Changing a property on a single object changes the value "for all threads" basically. There's no thread-specific copies of objects unless you make them yourself.

For multithreaded programs, the major challenge is making sure two threads aren't trying to access/write the same memory (a property, in your case) at the same time. Easiest way (but not necessarily most efficient, or fool-proof way) to do this for your property in question is to exclude the "nonatomic" attribute from your property declaration. (EDIT: this assumes you're using @synthesize to implement your properties, and not @dynamic nor have custom overriding getters or setters.)

Multi-threading is a bit of a large topic to cover here, but Apple's documentation is a good place to start for more info: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Multithreading/Introduction/Introduction.html

Upvotes: 3

Related Questions