diff --git a/native/cpp/CommonCpp/Tools/WorkerThread.h b/native/cpp/CommonCpp/Tools/WorkerThread.h --- a/native/cpp/CommonCpp/Tools/WorkerThread.h +++ b/native/cpp/CommonCpp/Tools/WorkerThread.h @@ -1,6 +1,7 @@ #pragma once #include <folly/MPMCQueue.h> +#include <future> #include <memory> #include <string> #include <thread> @@ -17,6 +18,7 @@ public: WorkerThread(const std::string name); void scheduleTask(const taskType task); + std::shared_ptr<std::promise<void>> flushAndBlock(); ~WorkerThread(); }; diff --git a/native/cpp/CommonCpp/Tools/WorkerThread.cpp b/native/cpp/CommonCpp/Tools/WorkerThread.cpp --- a/native/cpp/CommonCpp/Tools/WorkerThread.cpp +++ b/native/cpp/CommonCpp/Tools/WorkerThread.cpp @@ -26,6 +26,18 @@ } } +std::shared_ptr<std::promise<void>> WorkerThread::flushAndBlock() { + std::shared_ptr<std::promise<void>> queueBlockade = + std::make_shared<std::promise<void>>(std::promise<void>()); + std::promise<void> taskStarted; + this->tasks.write(std::make_unique<taskType>([queueBlockade, &taskStarted]() { + taskStarted.set_value(); + queueBlockade->get_future().get(); + })); + taskStarted.get_future().get(); + return queueBlockade; +} + WorkerThread::~WorkerThread() { this->tasks.blockingWrite(nullptr); try {