diff --git a/src/system_wrappers/source/cpu_windows.cc b/src/system_wrappers/source/cpu_windows.cc index 22146bf7aa..ccef55a2dd 100644 --- a/src/system_wrappers/source/cpu_windows.cc +++ b/src/system_wrappers/source/cpu_windows.cc @@ -67,8 +67,7 @@ CpuWindows::CpuWindows() // All resources are allocated in PollingCpu(). if (AllocateComplexDataTypes()) { - const bool success = StartPollingCpu(); - assert(success); + StartPollingCpu(); } else { @@ -135,30 +134,29 @@ void CpuWindows::DeAllocateComplexDataTypes() } } -bool CpuWindows::StartPollingCpu() +void CpuWindows::StartPollingCpu() { unsigned int dummy_id = 0; if (!cpu_polling_thread->Start(dummy_id)) { - return false; + initialize_ = false; + assert(false); } +} + +bool CpuWindows::StopPollingCpu() +{ { + // If StopPollingCpu is called immediately after StartPollingCpu() it is + // possible that cpu_polling_thread is in the process of initializing. + // Let initialization finish to avoid getting into a bad state. CriticalSectionScoped cs(*init_crit_); while(initialize_) { init_cond_->SleepCS(*init_crit_); } } - if (!has_initialized_) - { - cpu_polling_thread->Stop(); - return false; - } - return has_initialized_; -} -bool CpuWindows::StopPollingCpu() -{ if (!has_initialized_) { return false; @@ -204,7 +202,7 @@ bool CpuWindows::ProcessImpl() { has_initialized_ = false; terminate_ = true; - return false; + return true; } } // Approximately one seconds sleep for each CPU measurement. Precision is diff --git a/src/system_wrappers/source/cpu_windows.h b/src/system_wrappers/source/cpu_windows.h index 34f6181fff..d15073c0ba 100644 --- a/src/system_wrappers/source/cpu_windows.h +++ b/src/system_wrappers/source/cpu_windows.h @@ -45,7 +45,7 @@ private: bool AllocateComplexDataTypes(); void DeAllocateComplexDataTypes(); - bool StartPollingCpu(); + void StartPollingCpu(); bool StopPollingCpu(); static bool Process(void* thread_object);