objective-c - @synchronized(self) 是否创建一个 block ,其中 self 前缀在属性上是不必要的?

标签 objective-c synchronized self declared-property

我看了一些国外的代码,想验证一下我的假设:

@synchronized(self) 用于在设置属性时去掉 self 前缀。

所以在我下面的示例中,我正在设置实例的 strText,而不仅仅是局部变量,对吧?

- (void)myfunction{
    NSString * strText = @"var in function";
    @synchronized(self)
    {
         strText = @"var class (self.strText)";
    }

}

最佳答案

请阅读此Documentation

The @synchronized() directive locks a section of code for use by a single thread. Other threads are blocked until the thread exits the protected code—that is, when execution continues past the last statement in the @synchronized() block.

The @synchronized() directive takes as its only argument any Objective-C object, including self.

作为Massimo Cafaro pointed out : “最安全的做法是在应用程序变为多线程之前创建所有互斥对象,以避免竞争条件。”

关于objective-c - @synchronized(self) 是否创建一个 block ,其中 self 前缀在属性上是不必要的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4654487/

相关文章:

ios - 连接蓝牙耳机时无法切换到扬声器输出

ios - 二维 NSArray 中的对象数

ios - 如何将弹跳动画添加到 animateWithDuration?

ios - 丢失当前打开的 View 的引用

objective-c - 我该如何解决 "Collection was mutated while being enumerated"、 @synrchonized 、 mutableCopy 或其他问题?

静态变量上的 Java 同步方法

java同步问题

objective-c - 理解 Objective-C 中的 self

python - 在 Python 类中使用 self(在 Nuke 中制作 GUI)