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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:12:12+00:00 2026-06-01T12:12:12+00:00

In my program, I’ve got a bunch of classes that will only ever have

  • 0

In my program, I’ve got a bunch of classes that will only ever have one created instance for the duration of the program. They are created only once on initialization, and destroyed only once on closing.

These classes all tend to present really broad functionality that is used by many different parts of the program, and so having them as global externs is really clean and easy to understand, rather than encapsulating them in another class or passing around pointers.

The program is multi-threaded, and all of these classes contain their own mutex for access.

Are there any problems with this approach? If so, what are the recommended alternatives.

  • 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-06-01T12:12:13+00:00Added an answer on June 1, 2026 at 12:12 pm

    Use a singleton pattern as follows:

    Foo& foo() { static Foo x; return x; }
    

    This is completely thread-safe. See here: G++ 4.6 -std=gnu++0x: Static Local Variable Constructor Call Timing and Thread Safety

    It is guaranteed by the current standard.

    Furthermore this is superior to static initialized global variables because if you have multiple of the above they will be initialized lazily in the correct dependency order.

    Foo& foo() { static Foo x; return x; }
    
    Bar& bar() { static Bar x; return x; }
    

    and in Bar::Bar there is a call to foo(), then the Foo singleton will be created before Bar.

    If I had of used global variables:

    // Foo.cpp
    Foo foo;
    
    // Bar.cpp
    Bar bar;
    

    No such ordering is guaranteed.

    3.6.2 Initialization of non-local variables

    …the initialization of a variable is indeterminately sequenced with respect to the initialization of a variable defined in a different translation unit….

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

Sidebar

Related Questions

My program has a function that read/write file from resource. This function I have
My program has to have one color scheme for daylight hours and a darker
My program is a game that uses RMI to allow users to connect to
I program in Java and have been trying to understand exactly what operator overloading
The program that I am working on takes a file and parses it line
This program so far has one purpose, take in two integers(the size of the
Most program languages have some kind of exception handling; some languages have return codes,
Program A, is a c program that endlessly, receives input in stdin, process it
/* Program that reads a sequence of words from keyboard and prints the list
Program has more than one entry point defined: 'Class.Main()'. Compile with /main to specify

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.