Locking details of synthesized atomic @properties in Obj-C 2.0

前端 未结 2 1080
别跟我提以往
别跟我提以往 2021-01-02 05:45

The documentation for properties in Obj-C 2.0 say that atomic properties use a lock internally, but it doesn\'t document the specifics of the lock. Does anybody know if this

2条回答
  •  抹茶落季
    2021-01-02 06:19

    The lock used by atomic @properties is an implementation detail--for appropriate types on appropriate platforms, atomic operations without a lock are possible and I'd be surprised if Apple was not taking advantage of them. There is no public access to the lock in any case, so you can't @synchronize on the same lock. Several Apple engineers have pointed out that atomic properties do not guarantee thread safety; atomic properties only guarantee that gets/sets of that value are atomic. For correct thread safety, you will have to make use of higher-level locking or synchronization and you almost certainly would not want to use the same lock as the synthesize getter/setter(s) might be using.

提交回复
热议问题