22

I have read something in some foreign code and I want to check my assumption:

@synchronized(self) is used to get rid of the self prefix when setting a property.

So in my example below, I'm setting the strText of the instance, not just a local variable, right?

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

}
jscs
  • 63,694
  • 13
  • 151
  • 195
endo.anaconda
  • 2,449
  • 4
  • 29
  • 55

3 Answers3

41

Please read this 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.

As Massimo Cafaro pointed out: "It’s safest to create all the mutual exclusion objects before the application becomes multithreaded, to avoid race conditions."

Community
  • 1
  • 1
Tirth
  • 7,801
  • 9
  • 55
  • 88
  • 2
    The documentation link is now obsolete. Please refer to this one : https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Multithreading/ThreadSafety/ThreadSafety.html#//apple_ref/doc/uid/10000057i-CH8-SW1 – cescofry Apr 05 '13 at 10:26
  • @cescofry, Yes that link was outdated. I update my answer with your given link. Thanks. – Tirth Apr 05 '13 at 10:41
8

@synchronized(self) is used to get rid of the self. prefix.

So in my example I set the strText not in the function I set it in the class.

Two concepts are being conflated.

  1. @synchronized(self) { ... } only locks the block using the self object as the semaphore.
  2. In Objective-C, there is nothing like a hypothetical with statement as in other languages that removes the need for self.whatever to be just whatever. Might want to take the Stanford CS193P online course to brush up on the language.
newacct
  • 119,665
  • 29
  • 163
  • 224
  • 7
    Why this answer? It may 'answer' the question but--no offense--you just come across as terse and unhelpful. – Matt Mc Jun 29 '13 at 03:59
  • 5
    @synchronized has no effect on whether or not you need a "self." prefix. They are unrelated in any way. – Reid Ellis Mar 26 '14 at 16:23
  • 2
    This is the only answer of the three that actually addresses the misunderstanding in the question @Matt. The other two just parrot the docs about `@synchronized()` – jscs Oct 22 '15 at 22:36
  • @JoshCaswell Tracking, but my comment is from 2013, and in 2014 Barry edited the post to actually say something besides "you're wrong". I suppose you're right that this post now "answers the question" better. – Matt Mc Oct 24 '15 at 05:52
  • Oop, sorry Matt, I definitely should have thought to look at the revision history. – jscs Oct 24 '15 at 06:08
0

In a multithreaded environment if more than one thread tries to access same memory address may cause a “Race Condition”, to avoid such kind of conditions you should use “Mutex Lock(Mutual Exclusion)” nothing but blocking or restricting or locking n number of threads to access same memory address or content at a same point of time and allowing only one thread at an instance of time. This can be achieved in Objective C by using @synchronized directive.

Example: Generally while implementing Singleton design pattern or class you will see some kind of code snippet like below in any iOS projects,

+(id)getSingletonInstance
{
    @synchronized(self)
    {
        if (singletonObj == nil)
        {
            singletonObj = [[self alloc] init];
        }
        return singletonObj;
    }
}
Suraj K Thomas
  • 5,773
  • 4
  • 52
  • 64