Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5994823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:52:21+00:00 2026-05-22T23:52:21+00:00

Can someone please help me understand what the following method is doing? + (Game

  • 0

Can someone please help me understand what the following method is doing?

+ (Game *) shared
{
    static Game *sharedSingleton;

    @synchronized(self)
    {
        if (!sharedSingleton)
        {
            sharedSingleton = [[Game alloc] init];
        }
    }

    return sharedSingleton;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:52:22+00:00Added an answer on May 22, 2026 at 11:52 pm

    Obviously, the idea behind a singleton is to create only a single instance. The first step in achieving this is to declare a static instance of the class via the line static Game *sharedSingleton;.

    The second step is to check whether the single instance is already created, and if it isn’t, to create it, or if it is, to return the existing single instance. However, this second step opens up the potential for problems in the event that 2 separate threads try to call the +shared method at the same exact moment. You wouldn’t want one thread to be modifying the single sharedSingleton variable while another thread is trying to examine it, as it could produce unexpected results.

    The solution to this problem is to use the @synchronized() compiler directive to synchronize access to the object specified between the parenthesis. For example, say this single shared instance of the Game class has an instance variable named players which is an NSMutableArray of instances of a Player class. Let’s say the Game class had an -addPlayer: method, which would modify the players instance variable by adding the specified player. It’s important that if that method were called from multiple threads, that only one thread be allowed to modify the players array at a time. So, the implementation of that method might look something like this:

    - (void)addPlayer:(Player *)player {
       if (player == nil) return;
       @synchronized(players) {
          [players addObject:player];
       }
    }
    

    Using the @synchronized() directive makes sure only one thread can access the players variable at a time. If one thread attempts to while another thread is currently accessing it, the first thread must wait until the other thread finishes.

    While it’s more straightforward when you’re talking about an instance variable, it’s perhaps less clear how to achieve the same type of result within the single creation method of the class itself. The self in the @synchronized(self) line in the following code basically equates to the Game class itself. By synchronizing on the Game class, it assures that the sharedSingleton = [[Game alloc] init]; line is only ever called once.

    + (Game *) shared
    {
        static Game *sharedSingleton;
    
        @synchronized(self) // assures only one thread can call [Game shared] at a time
        {
            if (!sharedSingleton)
            {
                sharedSingleton = [[Game alloc] init];
            }
        }
    
        return sharedSingleton;
    }
    

    [EDIT]: updated. Based on my tests a while back (and I just re-tested it now), the following all appear to be equivalent:

    Outside @implementation:

    Game *sharedInstance;
    
    @implementation Game
    + (Game *)sharedGame {
        @synchronized(self) {
            if (sharedInstance == nil) {
                sharedInstance = [[[self class] alloc] init];
            }
        }
        return sharedInstance;
    }
    @end
    

    Outside @implementation, static:

    static Game *sharedInstance;
    
    @implementation Game
    + (Game *)sharedGame {
        @synchronized(self) {
            if (sharedInstance == nil) {
                sharedInstance = [[[self class] alloc] init];
            }
        }
        return sharedInstance;
    }
    @end
    

    Inside @implementation:

    @implementation Game
    
    static Game *sharedInstance;
    
    + (Game *)sharedGame {
        @synchronized(self) {
            if (sharedInstance == nil) {
                sharedInstance = [[[self class] alloc] init];
            }
        }
        return sharedInstance;
    }
    @end
    

    Inside +sharedGame:

    @implementation Game
    + (Game *)sharedGame {
        static Game *sharedInstance;
        @synchronized(self) {
            if (sharedInstance == nil) {
                sharedInstance = [[[self class] alloc] init];
            }
        }
        return sharedInstance;
    }
    @end
    

    The only difference is that in the first variation, without the static keyword, sharedInstance doesn’t show up under File Statics in gdb. And obviously, in the last variation, sharedInstance isn’t visible outside of the +sharedGame method. But practically speaking, they all assure that when you call [Game sharedInstance] you’ll get back the sharedInstance, and that the sharedInstance is only created once. (Note, however, that further precautions would be needed to prevent someone from creating a non-singleton instance using something like Game *game = [[Game alloc] init];).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please help me understand the following: In the previous version of NHibernate
Java docs says the following about Set interface, can someone please help me understand
Can someone please help me to understand what Am I doing wrong ? I
Can someone please help me figure out a way to achieve the following (see
Please can someone help? I have the following code which uploads a file to
Can someone please help be take apart the elements here and help me understand
With real examples and their use, can someone please help me understand: How to
Can someone please help me understand why this isn't working? The loading.gif appears while
Could someone please help me to understand why the following block of code keeps
Can someone please help me understand exactly what this means? <stmt> := var <ident>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.