Does @synchronized(self) create a block where the self prefix is unecessary on properties?
Asked Answered
G

3

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)";
    }

}
Greenhorn answered 11/1, 2011 at 5:23 Comment(0)
P
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."

Prothalamium answered 11/1, 2011 at 5:26 Comment(2)
The documentation link is now obsolete. Please refer to this one : developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/…Serinaserine
@cescofry, Yes that link was outdated. I update my answer with your given link. Thanks.Prothalamium
G
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.
Gloria answered 30/7, 2012 at 9:3 Comment(5)
Why this answer? It may 'answer' the question but--no offense--you just come across as terse and unhelpful.Sciomachy
@synchronized has no effect on whether or not you need a "self." prefix. They are unrelated in any way.Navaho
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()Claustral
@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.Sciomachy
Oop, sorry Matt, I definitely should have thought to look at the revision history.Claustral
P
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;
    }
}
Pammie answered 29/1, 2019 at 6:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.