ios @synchronized() 作为目标 C 中的单例方法有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6317889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:18:06  来源:igfitidea点击:

What does @synchronized() do as a singleton method in objective C?

objective-cioscocoa-touch

提问by max_

I just created a singleton method, and I would like to know what the function @synchronized()does, as I use it frequently, but do not know the meaning.

我刚刚创建了一个单例方法,我想知道这个函数@synchronized()是做什么的,因为我经常使用它,但不知道它的含义。

回答by John Calsbeek

It declares a critical sectionaround the code block. In multithreaded code, @synchronizedguarantees that only one thread can be executing that code in the block at any given time.

它在代码块周围声明了一个临界区。在多线程代码中,@synchronized保证在任何给定时间只有一个线程可以在块中执行该代码。

If you aren't aware of what it does, then your application probably isn't multithreaded, and you probably don't need to use it (especially if the singleton itself isn't thread-safe).

如果您不知道它的作用,那么您的应用程序可能不是多线程的,并且您可能不需要使用它(特别是如果单例本身不是线程安全的)。



Edit:Adding some more information that wasn't in the original answer from 2011.

编辑:添加一些 2011 年原始答案中没有的更多信息。

The @synchronizeddirective prevents multiple threads from entering any region of code that is protected by a @synchronizeddirective referring to the same object. The object passed to the @synchronizeddirective is the object that is used as the "lock." Two threads can be in the same protected region of code if a different object is used as the lock, and you can also guard two completely different regions of code using the same object as the lock.

@synchronized指令可防止多个线程进入受引用同一对象@synchronized指令保护的任何代码区域。传递给@synchronized指令的对象是用作“锁”的对象。如果使用不同的对象作为锁,则两个线程可以位于同一代码受保护区域,并且您还可以使用相同的对象作为锁来保护两个完全不同的代码区域。

Also, if you happen to pass nilas the lock object, no lock will be taken at all.

此外,如果您碰巧nil作为锁定对象传递,则根本不会获取锁定。

回答by csano

From the Apple documentation hereand here:

来自此处此处的 Apple 文档:

The @synchronized directive is a convenient way to create mutex locks on the fly in Objective-C code. The @synchronized directive does what any other mutex lock would do—it prevents different threads from acquiring the same lock at the same time.

@synchronized 指令是一种在 Objective-C 代码中动态创建互斥锁的便捷方式。@synchronized 指令做任何其他互斥锁会做的事情——它防止不同的线程同时获取相同的锁。

The documentation provides a wealth of information on this subject. It's worth taking the time to read through it, especially given that you've been using it without knowing what it's doing.

该文档提供了有关此主题的大量信息。花时间通读它是值得的,特别是考虑到您一直在使用它而不知道它在做什么。

回答by Durai Amuthan.H

The @synchronizeddirective is a convenient way to create mutex locks on the fly in Objective-Ccode.

@synchronized指令是一种在Objective-C代码中动态创建互斥锁的便捷方式。

The @synchronizeddirective does what any other mutex lock would do—it prevents different threads from acquiring the same lock at the same time.

@synchronized指令执行任何其他互斥锁会做的事情——它防止不同的线程同时获取相同的锁。

Syntax:

句法:

 @synchronized(key) 
 { 
  // thread-safe code 
 }

Example:

例子:

 -(void)AppendExisting:(NSString*)val
{
  @synchronized (oldValue) {
      [oldValue stringByAppendingFormat:@"-%@",val];
  }
}

Now the above code is perfectly thread safe..Now Multiple threads can change the value.

现在上面的代码是完全线程安全的..现在多个线程可以更改该值。

The above is just an obscure example...

以上只是一个模糊的例子......

回答by abdus.me

@synchronized block automatically handles locking and unlocking for you. @synchronize you have an implicit lock associated with the object you are using to synchronize. Here is very informative discussion on this topic please follow How does @synchronized lock/unlock in Objective-C?

@synchronized 块会自动为您处理锁定和解锁。@synchronize 你有一个与你用来同步的对象关联的隐式锁。这里有关于这个主题的非常有用的讨论,请遵循@synchronized 如何在Objective-C 中锁定/解锁?

回答by Matjan

Excellent answer here:

优秀的答案在这里:

Help understanding class method returning singleton

帮助理解返回单例的类方法

with further explanation of the process of creating a singleton.

进一步解释了创建单例的过程。

回答by Matjan

@synchronizedis thread safemechanism. Piece of code written inside this function becomes the part of critical section, to which only one thread can execute at a time.

@synchronizedthread safe机制。写在这个函数中的一段代码成为 的一部分critical section,一次只能执行一个线程。

@synchronizeapplies the lock implicitly whereas NSLockapplies it explicitly.

@synchronize隐式应用锁,而NSLock显式应用它。

It only assures the thread safety, not guarantees that. What I mean is you hire an expert driver for you car, still it doesn't guarantees car wont meet an accident. However probability remains the slightest.

它只保证线程安全,不保证。我的意思是你为你的车雇了一个专业的司机,但它仍然不能保证汽车不会发生事故。然而概率仍然是最小的。