From e529046d7d93fdeb65624ffe272c197b2c2905f0 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Tue, 4 Jun 2019 15:21:04 -0400 Subject: [PATCH] after further testing, ThreadSafe seems to be the fastest and most reliable --- GUI/host/textthread.cpp | 7 ++++--- GUI/host/textthread.h | 4 ++-- include/common.h | 1 + 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/GUI/host/textthread.cpp b/GUI/host/textthread.cpp index e7a130a..c047356 100644 --- a/GUI/host/textthread.cpp +++ b/GUI/host/textthread.cpp @@ -23,7 +23,7 @@ void TextThread::Stop() void TextThread::AddSentence(std::wstring&& sentence) { - queuedSentences.push(std::move(sentence)); + queuedSentences->emplace_back(std::move(sentence)); } void TextThread::Push(BYTE* data, int length) @@ -63,8 +63,9 @@ void TextThread::Flush() { if (storage->size() > 10'000'000) storage->erase(0, 8'000'000); // https://github.com/Artikash/Textractor/issues/127#issuecomment-486882983 - std::wstring sentence; - while (queuedSentences.try_pop(sentence)) + std::deque sentences; + queuedSentences->swap(sentences); + for (auto& sentence : sentences) { sentence.erase(std::remove(sentence.begin(), sentence.end(), L'\0')); if (Output(*this, sentence)) storage->append(sentence); diff --git a/GUI/host/textthread.h b/GUI/host/textthread.h index 75acb7f..945eae2 100644 --- a/GUI/host/textthread.h +++ b/GUI/host/textthread.h @@ -2,7 +2,7 @@ #include "common.h" #include "types.h" -#include +#include class TextThread { @@ -37,7 +37,7 @@ private: std::unordered_set repeatingChars; std::mutex bufferMutex; DWORD lastPushTime = 0; - concurrency::concurrent_queue queuedSentences; + ThreadSafe> queuedSentences; struct TimerDeleter { void operator()(HANDLE h) { DeleteTimerQueueTimer(NULL, h, INVALID_HANDLE_VALUE); } }; AutoHandle timer = NULL; }; diff --git a/include/common.h b/include/common.h index bd5572f..9005b28 100644 --- a/include/common.h +++ b/include/common.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include