-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsingleton.h
More file actions
42 lines (32 loc) · 685 Bytes
/
singleton.h
File metadata and controls
42 lines (32 loc) · 685 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#pragma once
#ifndef SINGLETON_H
#define SINGLETON_H
#include <pthread.h>
#include <memory>
#include <iostream>
template<typename T>
class Singleton
{
public:
static T & GetInstance()
{
pthread_once(&m_once, init);
return (*(m_ptInstance.get()));
}
private:
static void init()
{
m_ptInstance = std::unique_ptr<T>(new T());
//std::cout << "pthread_once init()" << std::endl;
}
Singleton() = delete;
~Singleton() = delete;
private:
static std::unique_ptr<T> m_ptInstance;
static pthread_once_t m_once;
};
template<typename T>
std::unique_ptr<T> Singleton<T>::m_ptInstance;
template<typename T>
pthread_once_t Singleton<T>::m_once = PTHREAD_ONCE_INIT;
#endif