#include "stdafx.h" #include "AutoThread.h" using namespace Sia::Api; CAutoThread::CAutoThread(const CSiaCurl& siaCurl) : _siaCurl(siaCurl.GetHostConfig()), _stopEvent(::CreateEvent(nullptr, FALSE, FALSE, nullptr)) { } CAutoThread::~CAutoThread() { StopAutoThread(); ::CloseHandle(_stopEvent); } void CAutoThread::StartAutoThread() { std::lock_guard l(_startStopMutex); if (!_thread) { _thread.reset(new std::thread([this]() { do { AutoThreadCallback(_siaCurl); } while (::WaitForSingleObject(_stopEvent, 2000) == WAIT_TIMEOUT); })); } } void CAutoThread::StopAutoThread() { std::lock_guard l(_startStopMutex); if (_thread) { ::SetEvent(_stopEvent); _thread->join(); _thread.reset(nullptr); } }