C++实现多线程安全的单例模式

根据自己另外那篇文章: 弄的:http://blog.chinaunix.net/uid-25958655-id-4297540.html
WINDOWS VC++2010下测试通过

MultiThreadSingleton.h:

  1. #ifndef MULTI_THREAD_SINGLETON_H_ 
  2. #define MULTI_THREAD_SINGLETON_H_

  3. #ifdef WIN32
  4. #include<iostream>
  5. #include<windows.h>

  6. #endif //WIN32



  7. #ifdef WIN32
  8. class locker
  9. {
  10. public: 
  11.     inline locker() { m_hMutex=CreateMutex(NULL,FALSE,NULL); std::cout<<"Locker::Locker() " ;} 
  12.     inline ~locker() { CloseHandle(m_hMutex); } 
  13.     inline void lock() { WaitForSingleObject(m_hMutex, INFINITE); } 
  14.     inline void unlock() { ReleaseMutex(m_hMutex); } 
  15. private: 
  16.     HANDLE m_hMutex;
  17. };
  18. #endif


  19. /* 
  20.  * this class is responsible for the running log of tinyJSE 
  21.  * there should only exist one instance of tinyLog, 
  22.  * so we use singleton to implement tinyLog 
  23.  */ 
  24. class tinyLog 
  25. { 
  26. public: 
  27.     static tinyLog *GetInstance(); 
  28.     static void WriteLog(const char *FORMAT,...); 
  29.     int iTestThread;
  30. private: 
  31.     tinyLog(); 
  32.     ~tinyLog(); 
  33. private: 
  34.         static tinyLog *log; //设置为static
  35.     static locker llock; 

  36. }; 

  37. #endif

MultiThreadSingleton.cpp:

点击(此处)折叠或打开

  1. #ifdef WIN32
  2. #include<iostream>
  3. #include<windows.h>

  4. #endif //WIN32


  5. #include "MultiThreadSingleton.h"



  6. tinyLog * tinyLog::log = NULL; 
  7. locker tinyLog::llock;

  8. tinyLog::tinyLog() 
  9. { 
  10.     printf("called tinyLog::tinyLog()\n");
  11. } 
  12.   
  13. tinyLog::~tinyLog() 
  14. { 
  15. } 
  16.   
  17. /* 
  18.  * get the pointer to the only instance of tinyLog 
  19.  * use double check to assure only one instance is created 
  20.  */ 
  21. tinyLog * tinyLog::GetInstance() 
  22. { 
  23.     if(NULL == log) 
  24.     {//double check 
  25.         llock.lock(); 
  26.         if(NULL == log) 
  27.         { 
  28.             log = new tinyLog(); //这里最好换成{ tinyLog * tmpLog=new tinyLog(); log=tmpLog; } 原因请看我另外一篇"C++中多线程Singleton的实现"
  29.             
  30.         } 
  31.         llock.unlock(); 
  32.     } 
  33.     return log; 
  34. } 
  35.   
  36. /* 
  37.  * Unified handling of the log of tinyJSE 
  38.  */ 
  39. void tinyLog::WriteLog(const char *FORMAT,...) 
  40. { 
  41.     va_list args; 
  42.   
  43.     va_start(args, FORMAT); 
  44.   
  45.     llock.lock(); 
  46.   
  47.     vfprintf(stdout,FORMAT,args); 
  48.   
  49.     llock.unlock(); 
  50.   
  51.     va_end(args); 
  52.   
  53. }

main.cpp:

  1. #ifdef WIN32
  2. #include<iostream>
  3. #include<windows.h>

  4. #endif



  5. #include "MultiThreadSingleton.h"

  6. DWORD WINAPI Fun1Proc(
  7.     LPVOID lpParameter
  8.     )
  9. {
  10.     std::cout<<"thread "<< (int)lpParameter<< ": " ;
  11.     tinyLog *pty1=tinyLog::GetInstance();
  12.     pty1->iTestThread ++;
  13.     std::cout<< "in Fun1Proc thread pty="<< pty1 << " "<< std::endl ;
  14.     Sleep(20000);
  15.     return 0;
  16. }



  17. int main()
  18. {
  19.     HANDLE hThread[10];


  20.     int i;
  21.     for(i=0; i<10; i++)
  22.         hThread[i] = CreateThread(NULL, 0, Fun1Proc, (LPVOID)i, 0, NULL);
  23.     std::cin>>i;

  24.     
  25.     return 0;
  26. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值