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 878863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:50:00+00:00 2026-05-15T11:50:00+00:00

I have the following class class Singleton { private: static Singleton *p_inst; Singleton(); public:

  • 0

I have the following class

class Singleton
{
  private:

    static Singleton *p_inst;
    Singleton();

  public:

    static Singleton * instance()
    {
      if (!p_inst)
      {
        p_inst = new Singleton();
      }

      return p_inst;
    }
};

Please do elaborate on precautions taken while implementing Singleton in multi-threaded environment .

  • 1 1 Answer
  • 1 View
  • 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-15T11:50:00+00:00Added an answer on May 15, 2026 at 11:50 am

    In multi-threading that clause

    if(!p_inst)
    {
        p_inst = new Singleton();
    }
    

    is actually 3 separate actions. You are getting the value of p_inst, setting the value of p_inst and writing the value of p_inst. So get-set-write means that you need to put a lock around p_inst otherwise you can have 2 threads which create a Singleton value that each thread uses.

    Here is how you can view the issue, assume that your Singleton has a mutable field val:

    thread A -> p_inst is NULL
        thread B -> p_inst is NULL
           thread A -> set to Singleton (1)
               thread B -> set to Singleton (2)
                  thread C -> p_inst is Singleton (2)
                      thread A -> set val to 4
                          thread B -> set val to 6
                             thread C -> get val (it's 6)
                                 thread A -> get val (it's 4!!)
    

    You see? There’s 2 copies of a Singleton floating about, neither of which knows about the other. The third thread which checks on the Singleton is only going to see the last assignment. But with locking, you can prevent multiple assignment and these types of problems.

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

Sidebar

Related Questions

In PHP, I have following Singleton Database Class: class Database { private static $instance;
I have the following Singleton Class in PHP. class CounterBalance{ private static $instance; private
So I have the following: public class Singleton { private Singleton(){} public static readonly
I have the following class: class risc { // singleton protected: static unsigned long
I have the following code implementation of my generic singleton provider: public sealed class
I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
I have a singleton with a following structure: // Hpp class Root : public
I have the following code: public class MessageEndpoint : IConfigureThisEndpoint, AsA_Server, IWantCustomInitialization { private
I have a class with the following structure: class Something { private static $_instance
I have designed following class <?php class DB { private static $objInstance; /* *

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.