关于c++:C并发与多线程-7单例设计模式callonce

5次阅读

共计 2783 个字符,预计需要花费 7 分钟才能阅读完成。

懒汉式单例类

#include <iostream>
#include <thread>
#include <mutex>

using namespace std;

class Singleton
{
public:
    static Singleton* getInstance();

    Singleton(const Singleton&) = delete;
    Singleton& operator=(const Singleton&) = delete;

private:
    Singleton() = default;

    static Singleton* instance;
    static mutex mtx;
};

Singleton *Singleton::instance = nullptr;
mutex Singleton::mtx;

Singleton* Singleton::getInstance()
{if (instance == nullptr)
    {lock_guard<mutex> lck(mtx);     // 留神这里!!!if (instance == nullptr)
        {instance = new Singleton();
        }
    }

    return instance;
}

void thread_func()
{Singleton* p = Singleton::getInstance();

    // p-> do something ...
}

int main()
{thread th1(thread_func);
    thread th2(thread_func);

    th1.join();
    th2.join();

    return 0;
}

饿汉式单例类

#include <iostream>
#include <thread>

using namespace std;

class Singleton
{
public:
    static Singleton* getInstance();

    Singleton(const Singleton&) = delete;
    Singleton& operator=(const Singleton&) = delete;

private:
    Singleton() = default;
    static Singleton* instance;
};

Singleton *Singleton::instance = new Singleton();   // 留神这里!!!Singleton* Singleton::getInstance()
{return instance;}

void thread_func()
{Singleton* p = Singleton::getInstance();

    // p-> do something ...
}

int main()
{thread th1(thread_func);
    thread th2(thread_func);

    th1.join();
    th2.join();

    return 0;
}

懒汉式的 call_once 实现

template <class Fn, class... Args>
  void call_once (once_flag& flag, Fn&& fn, Args&&... args);
  • 精确执行一次可调用对象 fn,即便同时从多个线程调用。
#include <iostream>
#include <thread>
#include <mutex>

using namespace std;

class Singleton
{
public:
    static Singleton* getInstance();

    Singleton(const Singleton&) = delete;
    Singleton& operator=(const Singleton&) = delete;

private:
    Singleton() = default;

    static void createInstance();

    static Singleton* instance;
    static once_flag onceFlag;
};

void Singleton::createInstance()
{instance = new Singleton();
}

Singleton *Singleton::instance = nullptr;
once_flag Singleton::onceFlag;

Singleton* Singleton::getInstance()
{call_once(onceFlag, createInstance);  // 留神这里!!!return instance;
}

void thread_func()
{Singleton* p = Singleton::getInstance();

    // p-> do something ...
}

int main()
{thread th1(thread_func);
    thread th2(thread_func);

    th1.join();
    th2.join();

    return 0;
}

单例类的资源清理【饿、懒同理】

#include <iostream>
#include <thread>
#include <mutex>

using namespace std;

class Singleton
{
    struct Recycle
    {~Recycle()       // 留神这里!!!{if (instance != nullptr)
            {
                delete instance;
                instance = nullptr;
            }
        }
    };

public:
    static Singleton* getInstance();

    Singleton(const Singleton&) = delete;
    Singleton& operator=(const Singleton&) = delete;

private:
    Singleton() = default;
    ~Singleton();

    static Singleton* instance;
    static mutex mtx;
};

Singleton *Singleton::instance = nullptr;
mutex Singleton::mtx;

Singleton* Singleton::getInstance()
{if (instance == nullptr)
    {lock_guard<mutex> lck(mtx);
        if (instance == nullptr)
        {instance = new Singleton();
            static Recycle recycle;
        }
    }

    return instance;
}

Singleton::~Singleton()     // 留神这里!!!{// 单例类资源清理}

void thread_func()
{Singleton* p = Singleton::getInstance();

    // p-> do something ...
}

int main()
{thread th1(thread_func);
    thread th2(thread_func);

    th1.join();
    th2.join();

    return 0;
}
正文完
 0