Advertisement
yinteresting9

Untitled

Oct 18th, 2018
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. class A
  2. {
  3. public:
  4. A();
  5. virtual ~A();
  6.  
  7. private:
  8. std::shared_ptr<ThreadClass1> tc1;
  9. }
  10.  
  11.  
  12. class ThreadClass1
  13. {
  14. public:
  15. ThreadClass1();
  16. virtual ~ThreadClass1();
  17.  
  18. private:
  19. std::thread fThread;
  20. }
  21.  
  22.  
  23. class CGenericLogger
  24. {
  25. public:
  26. CGenericLogger();
  27. virtual ~CGenericLogger();
  28.  
  29.  
  30. public:
  31. class CPrefixContext
  32. {
  33. friend class CGenericLogger;
  34. public:
  35. CPrefixContext(CGenericLogger& parent, const TXString& prefix) : fParent(parent) { fParent.farrPrefix.push_back( prefix ); }
  36. virtual ~CPrefixContext() { fParent.farrPrefix.pop_back(); }
  37.  
  38. protected:
  39. CGenericLogger& fParent;
  40. };
  41.  
  42. private:
  43. std::mutex fFlushBarrier;
  44. TXStringSTLArray farrPrefix;
  45. };
  46.  
  47.  
  48. =============================
  49. A.cpp
  50.  
  51. ********************************
  52. A::A()
  53. {
  54. tc1.reset(new ThreadClass1());
  55. }
  56. ********************************
  57. ============================
  58.  
  59.  
  60. ===============================
  61. ThreadClass1.cpp:
  62. ********************************
  63.  
  64.  
  65. std::shared_ptr<CGenericLogger> gLogger;
  66. CBynarySemaphore gThreadSemaphore;
  67.  
  68. ThreadClass1::ThreadClass1()
  69. {
  70. gLogger.reset( new CGenericLogger() );
  71. fThread = std::thread(_DownloadThread);
  72. }
  73. ThreadClass1::~ThreadClass1()
  74. {
  75. gThreadSemaphore.notify();
  76. fThread.join();
  77. gLogger.reset();
  78. }
  79.  
  80. static void _DownloadThread()
  81. {
  82. while(true)
  83. {
  84. gThreadSemaphore.wait();
  85. EThreadState state = EThreadState::Idle;
  86. NNA_SCOPE {
  87. std::lock_guard<std::mutex> block_threads_until_finish_this_job( gThreadDataBarier );
  88. state = gThreadData->fState;
  89. }
  90.  
  91. if ( state == EThreadState::Stop )
  92. break;
  93. else if ( state == EThreadState::NewData )
  94. {
  95. auto myLogger = gLogger;
  96. CGenericLogger::CPrefixContext loggerContext( *myLogger, "Something " );
  97. //do something;
  98. }
  99. }
  100. }
  101.  
  102.  
  103. ********************************
  104. ===============================
  105.  
  106. ======================================
  107. Main.cpp
  108. **************************************
  109.  
  110. A1 = A();
  111. A2 = A(); // Application crashes at line 97 when A2 dilaog is closed.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement