C++实现多线程安全的单例模式
来源:互联网 发布:元胞数组 编辑:程序博客网 时间:2024/06/05 22:29
根据自己另外那篇文章: 弄的:http://blog.chinaunix.net/uid-25958655-id-4297540.html
WINDOWS VC++2010下测试通过
MultiThreadSingleton.h:
MultiThreadSingleton.cpp:
main.cpp:
WINDOWS VC++2010下测试通过
MultiThreadSingleton.h:
- #ifndef MULTI_THREAD_SINGLETON_H_
- #define MULTI_THREAD_SINGLETON_H_
- #ifdef WIN32
- #include<iostream>
- #include<windows.h>
- #endif //WIN32
- #ifdef WIN32
- class locker
- {
- public:
- inline locker() { m_hMutex=CreateMutex(NULL,FALSE,NULL); std::cout<<"Locker::Locker() " ;}
- inline ~locker() { CloseHandle(m_hMutex); }
- inline void lock() { WaitForSingleObject(m_hMutex, INFINITE); }
- inline void unlock() { ReleaseMutex(m_hMutex); }
- private:
- HANDLE m_hMutex;
- };
- #endif
- /*
- * this class is responsible for the running log of tinyJSE
- * there should only exist one instance of tinyLog,
- * so we use singleton to implement tinyLog
- */
- class tinyLog
- {
- public:
- static tinyLog *GetInstance();
- static void WriteLog(const char *FORMAT,...);
- int iTestThread;
- private:
- tinyLog();
- ~tinyLog();
- private:
- static tinyLog *log; //设置为static
- static locker llock;
- };
- #endif
MultiThreadSingleton.cpp:
点击(此处)折叠或打开
- #ifdef WIN32
- #include<iostream>
- #include<windows.h>
- #endif //WIN32
- #include "MultiThreadSingleton.h"
- tinyLog * tinyLog::log = NULL;
- locker tinyLog::llock;
- tinyLog::tinyLog()
- {
- printf("called tinyLog::tinyLog()\n");
- }
-
- tinyLog::~tinyLog()
- {
- }
-
- /*
- * get the pointer to the only instance of tinyLog
- * use double check to assure only one instance is created
- */
- tinyLog * tinyLog::GetInstance()
- {
- if(NULL == log)
- {//double check
- llock.lock();
- if(NULL == log)
- {
- log = new tinyLog(); //这里最好换成{ tinyLog * tmpLog=new tinyLog(); log=tmpLog; } 原因请看我另外一篇"C++中多线程Singleton的实现"
-
- }
- llock.unlock();
- }
- return log;
- }
-
- /*
- * Unified handling of the log of tinyJSE
- */
- void tinyLog::WriteLog(const char *FORMAT,...)
- {
- va_list args;
-
- va_start(args, FORMAT);
-
- llock.lock();
-
- vfprintf(stdout,FORMAT,args);
-
- llock.unlock();
-
- va_end(args);
-
- }
main.cpp:
- #ifdef WIN32
- #include<iostream>
- #include<windows.h>
- #endif
- #include "MultiThreadSingleton.h"
- DWORD WINAPI Fun1Proc(
- LPVOID lpParameter
- )
- {
- std::cout<<"thread "<< (int)lpParameter<< ": " ;
- tinyLog *pty1=tinyLog::GetInstance();
- pty1->iTestThread ++;
- std::cout<< "in Fun1Proc thread pty="<< pty1 << " "<< std::endl ;
- Sleep(20000);
- return 0;
- }
- int main()
- {
- HANDLE hThread[10];
- int i;
- for(i=0; i<10; i++)
- hThread[i] = CreateThread(NULL, 0, Fun1Proc, (LPVOID)i, 0, NULL);
- std::cin>>i;
-
- return 0;
- }
0 0
- 多线程单例模式的安全实现
- 单例模式的多线程安全支持(C++)
- C++实现多线程安全的单例模式
- C++实现多线程安全的单例模式
- C++实现多线程安全的单例模式 已测试
- 一种多线程安全的单例模式
- 单例模式的多线程安全
- 多线程安全的Singleton单件模式在C++,java与C#下的实现
- 【Java多线程】之八:单例模式的线程安全
- 多线程安全的单件模式,模板基类的单件实现
- 单例的多线程安全
- 设计模式 - 线程安全的单例模式(C#)
- 单例模式和多线程安全
- 单例模式和多线程安全
- 多线程安全(二)之单例模式
- 如何实现多线程安全的单件
- 多线程下安全的单例模式(双重校验锁实现,登记者模式实现,利用内部类实现,利用枚举实现)
- object-c 单例模式的线程安全
- qt显示图片汇总
- pomelo生命周期回调和组件添加
- java加强之反射技术
- Linux下zip压缩文件时怎样排除指定的文件
- 图像处理和图像识别中常用的OpenCV函数
- C++实现多线程安全的单例模式
- linux下安装飞信机器人教程
- GestureDetector封装手势检测上下滑动
- Android Framework内核之旅
- 版本控制-git(二)
- Redis 3.0 源码注释
- Bellman-Ford算法和Dijkstra算法
- OpenCV数据格式转换成Halcon数据格式HObject
- Java:类与继承