Multi-Threading question in Objective-C 2.0

前端 未结 3 754
谎友^
谎友^ 2021-02-11 05:52

I have my main application delegate which contains a method that returns an object. This application delegate runs on the main thread.

I also have a NSOperation that get

3条回答
  •  旧巷少年郎
    2021-02-11 06:14

    Unless you explicitly write code to cause something to execute on another thread, every method call is going to be executed directly on the thread it was called upon. There is no magic with a method call. You can think of it as having the exact same semantics/ABI as a C function call for the purposes of threading.

    Your locking pattern will work fine to ensure exclusive access across threads.

    Two additional unrelated notes (because so many people trip over it):

    • declaring a property as atomic has little to do with thread safety. Atomicity only guarantees that you get a valid value, not the correct value (there is a difference).

    • autoreleased objects are never safe to pass between threads. You need an explicit retain on the sending thread and a balancing eventual release on the receiving thread.

提交回复
热议问题