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

Objective CIosCocoa Touch

Objective C Problem Overview


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.

Objective C Solutions


Solution 1 - Objective C

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

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.

The @synchronized directive prevents multiple threads from entering any region of code that is protected by a @synchronized directive referring to the same object. The object passed to the @synchronized directive 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.

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

Solution 2 - Objective C

From the Apple documentation here and here:

> 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.

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.

Solution 3 - Objective C

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.

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...

Solution 4 - Objective C

@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 https://stackoverflow.com/q/1215330/1714082

Solution 5 - Objective C

Excellent answer here:

https://stackoverflow.com/questions/6234344/help-understanding-class-method-returning-singleton

with further explanation of the process of creating a singleton.

Solution 6 - Objective C

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

@synchronize applies the lock implicitly whereas NSLock applies it explicitly.

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmax_View Question on Stackoverflow
Solution 1 - Objective CJohn CalsbeekView Answer on Stackoverflow
Solution 2 - Objective CcsanoView Answer on Stackoverflow
Solution 3 - Objective CDurai Amuthan.HView Answer on Stackoverflow
Solution 4 - Objective Cabdus.meView Answer on Stackoverflow
Solution 5 - Objective CMatjanView Answer on Stackoverflow
Solution 6 - Objective Cuser3693546View Answer on Stackoverflow