Sponsored Content
Full Discussion: C++ singleton
Top Forums Programming C++ singleton Post 302727763 by DreamWarrior on Tuesday 6th of November 2012 09:51:53 PM
Old 11-06-2012
I'm fond of this method....

Code:
class Single
{
public:
  static Single &getSingle()
  {
    static Single theSingle;
    return theSingle;
  }

private:
  Single();  // private constructor to enforce singleton

//public:  // (1) if you uncomment this line then (2) below
           //       can be made to compile (and crash!).
  ~Single();
};

Single::Single()
{
}

Single::~Single()
{
}

int main()
{
  Single &s = Single::getSingle();
//  delete &s;  // (2) won't compile if you uncomment unless you
                //     uncomment (1) above; but, who would do this?
  return 0;
}

This has the benefit that the reference can't be freed (though, as I show, the caller can jump through hoops to free it, so the private destructor is still good practice). However, I'd suspect that most people would see a reference and know that deleting it is probably bad.

Buyer beware, however, that the standard is pretty hush-hush on its thread safety. Though, most practical compilers (g++) wrap the local static member ("theSingle") with locks to insure it does not get constructed more than once. In my opinion, this is preferable to roll-your-own. However, if you must, a pthread_once_t control is probably the way to do it as here:

Code:
#include <pthread.h>

class Single
{
public:
  static Single &getSingle()
  {
     pthread_once(&onceCtl, BuildTheSingle);
     return *theSingle;
  }

private:
  static Single *theSingle;
  static pthread_once_t onceCtl;
  static void BuildTheSingle();

  Single();
  ~Single();
};

pthread_once_t Single::onceCtl = PTHREAD_ONCE_INIT;
Single *Single::theSingle = NULL;

void Single::BuildTheSingle()
{
  theSingle = new Single();
}

Single::Single()
{
}

Single::~Single()
{
}

Which uses the same reference return to, IMO, instruct the caller that it's not theirs and they shouldn't be free'ing it.

I suppose you could also use auto pointers or something, too, but...this is good enough for me, lol.

edit: for the record, the former implementation is thread-safe in C++0x, as such, in any newer code compiled with C++0x compliant compiler, I'd recommend it to rid all the boilerplate crap and distill it right to only that code which is both sensible and required. Just one man's opinion....

Last edited by DreamWarrior; 11-06-2012 at 11:11 PM..
 

3 More Discussions You Might Find Interesting

1. Programming

What is singleton class ?

hello members, What is singleton class in c++ and in which case we will go with singleton class. Thanks & Regards Rajkumar_g (2 Replies)
Discussion started by: rajkumar_g
2 Replies

2. Programming

C++ abstract (singleton) factory implementation...

I want to create an abstract factory template which will allow me to pass in an "ID" for a subclass and return the singleton instance of that class stored in the factory. It'd be easy to adapt for "multi-ton", but for its present use this isn't necessary. The requirements are: - I don't want... (2 Replies)
Discussion started by: DreamWarrior
2 Replies

3. UNIX for Beginners Questions & Answers

AIX errpt : ( E7A89C7D Local adapter disabled after unstable singleton for long time )

Medel : 9117-MMC OS: AIX 6.1 Patch level : 6100-07-04-1216 Hacmp version : HACMP v 6.1.0.8 Oracle : 11.2.0.3 RAC Node : 2 node Dear, my one node server has been restarted early this morning, So, i tried to start HA and Oracle database. after that, the follow error appears at the node... (1 Reply)
Discussion started by: tomato00
1 Replies
Net::Server::MultiType(3pm)				User Contributed Perl Documentation			       Net::Server::MultiType(3pm)

NAME
Net::Server::MultiType - Net::Server personality SYNOPSIS
use base qw(Net::Server::MultiType); sub process_request { #...code... } my @types = qw(PreFork Fork Single); Net::Server::MultiType->run(server_type => @types); DESCRIPTION
Please read the pod on Net::Server first. This module is a personality, or extension, or sub class, of the Net::Server module. This personality is intended to allow for easy use of multiple Net::Server personalities. Given a list of server types, Net::Server::MultiType will require one at a time until it finds one that is installed on the system. It then adds that package to its @ISA, thus inheriting the methods of that personality. ARGUMENTS
In addition to the command line arguments of the Net::Server base class, Net::Server::MultiType contains one other configurable parameter. Key Value Default server_type 'server_type' 'Single' server_type May be called many times to build up an array or possible server_types. At execution, Net::Server::MultiType will find the first available one and then inherit the methods of that personality CONFIGURATION FILE
"Net::Server::MultiType" allows for the use of a configuration file to read in server parameters. The format of this conf file is simple key value pairs. Comments and white space are ignored. #-------------- file test.conf -------------- ### multi type info ### try PreFork first, then go to Single server_type PreFork server_type Single ### server information min_servers 20 max_servers 80 spare_servers 10 max_requests 1000 ### user and group to become user somebody group everybody ### logging ? log_file /var/log/server.log log_level 3 pid_file /tmp/server.pid ### access control allow .+.(net|com) allow domain.com deny a.+ ### background the process? background 1 ### ports to bind host 127.0.0.1 port localhost:20204 port 20205 ### reverse lookups ? # reverse_lookups on #-------------- file test.conf -------------- PROCESS FLOW
See Net::Server HOOKS
There are no additional hooks in Net::Server::MultiType. TO DO
See Net::Server AUTHOR
Paul T. Seamons paul@seamons.com SEE ALSO
Please see also Net::Server::Fork, Net::Server::INET, Net::Server::PreFork, Net::Server::MultiType, Net::Server::Single perl v5.14.2 2012-06-05 Net::Server::MultiType(3pm)
All times are GMT -4. The time now is 02:33 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy