Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1744550
  • 博文数量: 263
  • 博客积分: 1218
  • 博客等级: 少尉
  • 技术积分: 2862
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-19 02:33
文章分类

全部博文(263)

文章存档

2020年(12)

2019年(2)

2018年(10)

2016年(1)

2015年(20)

2014年(115)

2013年(46)

2012年(37)

2011年(20)

分类: Windows平台

2014-06-11 17:37:58

根据自己另外那篇文章: 弄的: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. }








以下是运行结果: 虽然结果看起来杂乱无章: 但是我们看到tinyLog()构造函数中只被调用的了一次, 且他们的地址都相同:

  1. Locker::Locker() thread thread 0thread thread thread thread thread 2thread threa
  2. d thread : 37481: 569called tinyLog::tinyLog()
  3. : : : : : : : : in Fun1Proc thread pty=in Fun1Proc thread pty=in Fun1Proc thread
  4.  pty=in Fun1Proc thread pty=in Fun1Proc thread pty=in Fun1Proc thread pty=in Fun
  5. 1Proc thread pty=in Fun1Proc thread pty=in Fun1Proc thread pty=in Fun1Proc threa
  6. d pty=00397250003972500039725000397250003972500039725000397250003972500039725000
  7. 397250


阅读(5098) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~