11 #include "WorkerThread.h"
12 #include "../../FaissAssert.h"
14 namespace faiss {
namespace gpu {
16 WorkerThread::WorkerThread() :
30 WorkerThread::startThread() {
31 thread_ = std::thread([
this](){ threadMain(); });
36 std::lock_guard<std::mutex> guard(mutex_);
39 monitor_.notify_one();
44 std::lock_guard<std::mutex> guard(mutex_);
50 auto fut = p.get_future();
57 auto pr = std::promise<bool>();
58 auto fut = pr.get_future();
60 queue_.emplace_back(std::make_pair(std::move(f), std::move(pr)));
63 monitor_.notify_one();
68 WorkerThread::threadMain() {
72 FAISS_ASSERT(wantStop_);
74 for (
auto& f : queue_) {
76 f.second.set_value(
true);
81 WorkerThread::threadLoop() {
83 std::pair<std::function<void()>, std::promise<bool>> data;
86 std::unique_lock<std::mutex> lock(mutex_);
88 while (!wantStop_ && queue_.empty()) {
96 data = std::move(queue_.front());
101 data.second.set_value(
true);
void stop()
Request that the worker thread stop itself.
std::future< bool > add(std::function< void()> f)