mirror of
https://github.com/caozhiyi/CppNet.git
synced 2026-01-19 00:05:14 +08:00
30 lines
619 B
C++
30 lines
619 B
C++
#ifndef HEADER_BASE_SINGLE
|
|
#define HEADER_BASE_SINGLE
|
|
|
|
#include <mutex>
|
|
|
|
namespace base {
|
|
|
|
template<typename T>
|
|
class CSingle {
|
|
private:
|
|
CSingle(const CSingle&);
|
|
CSingle& operator = (const CSingle&);
|
|
public:
|
|
CSingle() {}
|
|
virtual ~CSingle() {}
|
|
|
|
static T& Instance() {
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
static T instance;
|
|
return instance;
|
|
}
|
|
|
|
private:
|
|
static std::mutex _mutex;
|
|
};
|
|
|
|
template<typename T>
|
|
std::mutex CSingle<T>::_mutex;
|
|
}
|
|
#endif |