89

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.

George Stocker
  • 57,289
  • 29
  • 176
  • 237
max_
  • 24,076
  • 39
  • 122
  • 211

6 Answers6

120

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.

John Calsbeek
  • 35,947
  • 7
  • 94
  • 101
  • 14
    A couple of important points: 1) If you use a nil pointer in `@synchronized` it does nothing -- you're left unprotected. 2) `@synchronized` is *slow*. – Hot Licks Jul 27 '13 at 13:02
  • This answer is misleading, and should not be the accepted answer. Although what it says would sometimes be correct (as long as the token passed to *synhronized* is the same object in all threads), it is misleadingly incomplete. *synchronized* prevents any number of associated code sections from executing at the same time, not just "that code in the block". The parameter to *synchronized* effectively determines what sections of code (or "blocks" as the answer calls them) are protected from concurrent access. – Arda Mar 20 '17 at 17:20
  • @Arda You're totally right. I've added a little bit more information and a link to some Apple documentation about `@synchronized`. – John Calsbeek Mar 22 '17 at 03:19
  • @JohnCalsbeek, the answer looks much better now. Thumbs up from me. – Arda Mar 23 '17 at 04:19
  • @HotLicks interesting to point this out, but would have been even better to say briefly what could be the alternatives (links?) – itMaxence Nov 06 '18 at 18:53
43

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.

csano
  • 13,266
  • 2
  • 28
  • 45
27

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

Durai Amuthan.H
  • 31,670
  • 10
  • 160
  • 241
6

@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?

Community
  • 1
  • 1
abdus.me
  • 1,819
  • 22
  • 34
4

Excellent answer here:

Help understanding class method returning singleton

with further explanation of the process of creating a singleton.

Community
  • 1
  • 1
Matjan
  • 3,591
  • 1
  • 33
  • 31
-2

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

  • 2
    This is absolutely wrong. dispatch_once DOES NOT do the same as @syncrhonized, it can be a substitute ONLY under the allocation of a singleton. – jugutier Aug 22 '16 at 21:32