9 #include "WorkerThread.h"
10 #include "FaissAssert.h"
18 void runCallback(std::function<
void()>& fn,
19 std::promise<bool>& promise) {
22 promise.set_value(
true);
24 promise.set_exception(std::current_exception());
30 WorkerThread::WorkerThread() :
44 WorkerThread::startThread() {
45 thread_ = std::thread([
this](){ threadMain(); });
50 std::lock_guard<std::mutex> guard(mutex_);
53 monitor_.notify_one();
58 std::lock_guard<std::mutex> guard(mutex_);
64 auto fut = p.get_future();
71 auto pr = std::promise<bool>();
72 auto fut = pr.get_future();
74 queue_.emplace_back(std::make_pair(std::move(f), std::move(pr)));
77 monitor_.notify_one();
82 WorkerThread::threadMain() {
86 FAISS_ASSERT(wantStop_);
89 for (
auto& f : queue_) {
90 runCallback(f.first, f.second);
95 WorkerThread::threadLoop() {
97 std::pair<std::function<void()>, std::promise<bool>> data;
100 std::unique_lock<std::mutex> lock(mutex_);
102 while (!wantStop_ && queue_.empty()) {
110 data = std::move(queue_.front());
114 runCallback(data.first, data.second);
void stop()
Request that the worker thread stop itself.
std::future< bool > add(std::function< void()> f)