(vii) The single-instance class of the Boost library

Source: Internet
Author: User

(vii) The single-instance class of the Boost library

One, boost.serialzation single-piece realization

Singleton mode is a kind of common software design pattern. In its core structure, it contains only a special class called a singleton class. The singleton mode can ensure that there is only one instance of a class in the system, and the instance is easy to be accessed by the outside world, thus it is convenient to control the number of instances and save system resources. Singleton mode is the best solution if you want to have only one object for a class in the system.

Singleton, typically a single case in a large project, is very common, and the boost library does not provide a specialized singleton class, but it can be found in other libraries

#
Public boost::serialization::singleton<threadpoll>
{
};

Of course, you can also not use the inheritance of the way, only a typedef, you can get a global access point, this way will be more flexible.

ClassThreadpoll
{
};
Boost:: Serialization:threadpollagent;  

However, the singleton is non-thread-safe, and a single-piece class implements a global access point that, if accessed in multiple threading environments, is self-locking, so if you need a thread-safe single piece, you need to implement it yourself.

Two, self-realization thread-safe single piece

/***************************** **** **** **** **** **** **** * 
* Thread-safe single-instance classes
****************************** **** **** **** **** **** **** */ 
#ifndef __singleton_h__
#define __singleton_h__
#include <boost/noncopyable.hpp>
#include <boost/thread.hpp>
Sentinel class, responsible for multi-threaded operation, automatic lock unlock
Sentinel class does not allow copying,
Template<typename t>  
Class Singletonguard:boost::mutex::scoped_lock, Public boost::noncopyable
{
Public
    Explicit Singletonguard (T* Inst, boost::mutex& MT): Boost::mutex::scoped_lock (MT), M_guardptr (inst)
    {         
    }
    T* operator-> ()
    {
        return m_guardptr;
    }
Private
    T* M_GUARDPTR;
};
Monitoring class for monitoring the status of a singleton
Template<typename t>  
Class Singleton_wrapper:public T
{
Public
    static bool m_is_destroyed;
    ~singleton_wrapper () {
        M_is_destroyed = true;
    }
};
Template<typename t>  
BOOL Singleton_wrapper< T >::m_is_destroyed = false;
Single case
Template<typename t>  
Class Singleton:public Boost::noncopyable
{
Public
    Static Singletonguard<T> get_mutable_instance () {
        return Singletonguard<T> (&get_instance (), M_signalmutex);
    }
    static const T & Get_const_instance () {
        return Get_instance ();
    }
Private
    static T & instance;
    static void Use (T-const &) {}
    Static T & Get_instance () {
        Static Singleton_wrapper< T > t;
        Initializing an instance at compile time
        Boost_assert (! Singleton_wrapper< T >::m_is_destroyed);
        Use (instance);
        return static_cast<t &> (T);
    }
    
Protected
    Boost::mutex::scoped_lock Scopedlock ()
    {
        Return Boost::mutex::scoped_lock (M_signalmutex);
    }
};
Template<typename t>  
Boost::mutex Singleton
Template<typename t>  
T & Singleton< T >::instance = Singleton< T >::get_instance (); 
__singleton_h__

This class references the implementation of the boost single piece, similar in use, but get_mutable_instance returns a temporary variable that is not an instance object, but saves the instance pointer by defining a function that accesses the instance through the symbol-----the temporary variable is destructor when it leaves the scope,

Unlock it yourself. All functions accessed through the singleton are accessed in the form of a lock, and if you want to access without locking, you can only access the instance by Get_const_instance.

You can use it as follows:

ClassThreadpoll
{
Public
    void Update () {};
    int Query ()const{};
};
typedef singleton<threadpoll> Threadpollagent;
Threadpollagent::get_mutable_instance ()->update ();
Threadpollagent::get_const_instance (). Query ();

(vii) The single-instance class of the Boost library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.