diff --git a/host/textthread.cc b/host/textthread.cc index 026889b..2146ebf 100644 --- a/host/textthread.cc +++ b/host/textthread.cc @@ -13,17 +13,20 @@ #define TT_LOCK std::lock_guard ttLocker(ttMutex) // Synchronized scope for accessing private data TextThread::TextThread(ThreadParameter tp, DWORD status) : - status(status), + deletionEvent(CreateEventW(nullptr, FALSE, FALSE, NULL)), + flushThread([&]() { while (WaitForSingleObject(deletionEvent, 100), FlushSentenceBuffer()); }), timestamp(GetTickCount()), Output(nullptr), tp(tp), - flushThread([&]() { while (Sleep(25), FlushSentenceBuffer()); }) + status(status) {} TextThread::~TextThread() { status = -1UL; + SetEvent(deletionEvent); flushThread.join(); + CloseHandle(deletionEvent); } std::wstring TextThread::GetStore() diff --git a/host/textthread.h b/host/textthread.h index 8558dca..0f4d6b6 100644 --- a/host/textthread.h +++ b/host/textthread.h @@ -47,14 +47,15 @@ public: private: bool FlushSentenceBuffer(); - ThreadOutputCallback Output; std::vector sentenceBuffer; std::wstring storage; - - ThreadParameter tp; std::recursive_mutex ttMutex; + HANDLE deletionEvent; std::thread flushThread; + DWORD timestamp; + ThreadOutputCallback Output; + ThreadParameter tp; DWORD status; };