Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 845
max_ Avatar asked Jun 11 '11 18:06

max_


People also ask

What does synchronized do when applied to a method?

Synchronized methods enable a simple strategy for preventing thread interference and memory consistency errors: if an object is visible to more than one thread, all reads or writes to that object's variables are done through synchronized methods.

Why we use synchronized on the block when we can use it on the method?

We need to synchronize the shared resources to ensure that at a time only one thread is able to access the shared resource. If an Object is shared by multiple threads then there is need of synchronization in order to avoid the Object's state to be getting corrupted. Synchronization is needed when Object is mutable.

Why do we use synchronized IOS?

The presence of multiple threads in an application opens up potential issues regarding safe access to resources from multiple threads of execution. Two threads modifying the same resource might interfere with each other in unintended ways.

Why would you want to use synchronized blocks instead of synchronized methods?

synchronized block has better performance as only the critical section is locked but synchronized method has poor performance than block. synchronized block provide granular control over lock but synchronized method lock either on current object represented by this or class level lock.


2 Answers

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.

like image 80
John Calsbeek Avatar answered Oct 15 '22 18:10

John Calsbeek


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.

like image 43
csano Avatar answered Oct 15 '22 17:10

csano