last day (14 days later) » 

11:19
0
Q: Is @property (readonly, nonatomic) thread-safe for public reads once initialized?

LocutusDoes this guarantee that any thread accessing the name property will see the value set in initWithName immediately after the object is instantiated? Is atomic required? _name will only ever be assigned once in the initializer. I know that any thread started from code after the object is created ...

@KiritModi I know that! In this case I am assigning the variable before the object is even returned and it's never written to again. Can I set a memory barrier after assignment?
@Locutus You're assigning what variable? Please show your use-case.
@trojanfoe This is the entire class. It's public. Other classes will read the name property. _name is the auto-generated ivar.
It's still confusing; you say other threads "that somehow referenced the object", however the object has no reference anywhere until init returns, so how can any thread have a reference?
@trojanfoe Say I initialize it and pass it to an already running message loop thread.
11:19
How? Show the code.
I haven't written it yet.
I'm going to run a message loop in a background thread, and these objects will be initialized on the main thread and sent to the message loop. The message loop thread would have to read the name property.
it's impossible for the other thread to even see the reference to the object until init has returned.
Yes. After init returns, I will push the object onto a queue.
And the other thread which is already running will retrieve it.
The queue is thread-safe, but that only guarantees the safety of the pointers it holds, not the contents of the object.
So you've answered your question; other threads can see property set in init, but not until it's returned. The nonatomic keyword doesn't affect the situation here.
How can other threads see it IMMEDIATELY?
It could be in the cpu cache.
Unless it's synchronized or volatile.
11:25
No, because there is no reference anywhere until init has returned
Don't I have to enforce some kind of memory barrier after setting it?
But the reference is only to the object.
It's contents, such as the name property is another object.
Why? How can another thread get a reference to an object that hasn't even finished being initialised?
I've already initialized it!
OK - I must be missing something here.
What is the guarantee that the child objects that were created during initialization are visible to other threads?
11:26
Your question is not complete.
I seem unable to explain myself and I will let someone else take over.
The object is called Transaction.
It has a property called 'name'.
After init returns a Transaction object has successfully been created.
I can access 'name' from the same thead that created the Transaction object.
But if I pass the Transaction object to another thread, they can only see Transaction. It's not guaranteed that they can see 'name'.
Or the latest value of 'name'.

last day (14 days later) »