Code Monkey home page Code Monkey logo

globalsingleton's Introduction

GlobalSingleton

一个进程空间内全局单例的框架 - 支持动态库热加载/热卸载

Windows使用注意事项:
1. 要求使用MD/MDd,而不是MT/MTd,保证exe和所有dll间使用同一个crtheap堆,否则
   exe和不同dll间都有各自的crtheap堆,而new和delete需要在同一堆中配套执行。
2. 慎用virtual,析构函数不能为virtual函数。因为当析构函数为virtual函数时,如果
   单例由dll1加载,而dll2在dll1卸载之后如果还在使用,那析构时调virtual的析构函
   数会去查虚函数表,而虚函数表是由dll1创建的,会引发崩溃。其他虚函数也涉及虚函
   数表,因此若要使用虚函数,那么除非能保证dll的卸载顺序,否则不要使用热卸载。
   事实上,全局单例的管理交由单例框架来实现后,析构函数是否使用virtual都不会产生
   泄漏,因为单例框架构造和析构时使用的都是具体的全局单例类,而不会是它们的基类。
3. 单例全为懒加载,直到GetReference的时候才真正实例化单例对象,需要注意全局单例
   没有保证多线程间安全,因此在单例实例化/动态库首次获取单例时都是线程不安全的。
   若在SingletonManager.cpp的Count/Obtain/Release函数中使用std::mutex加锁能够实现
   单例获取的安全,但实例化过程(创建过程)仍是线程不安全的。

C++版本要求:C++14 以上(MSVC v140 (VS2015) 以上,如果使用Visual Studio)。

globalsingleton's People

Contributors

kondeu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

globalsingleton's Issues

typeid(T).hash_code在某些编译器、不同类型或不同版本编译器上有潜在风险

typeid(T).hash_code在Android的部分编译器上同一个类型在不同的so中的hash_code会不同,会导致GlobalSingleton无法工作;

若最终的dll/so集合混用了不同类型、不同版本的编译器,可能会有hash_code不一致的潜在风险。

"Obtain", typeid(T).hash_code(), sizeof(T));

"Release", typeid(T).hash_code());

"Count", typeid(T).hash_code()) > 0);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.