10 #include "WorkerThread.h"
11 #include "../../FaissAssert.h"
13 namespace faiss {
namespace gpu {
15 WorkerThread::WorkerThread() :
29 WorkerThread::startThread() {
30 thread_ = std::thread([
this](){ threadMain(); });
35 std::lock_guard<std::mutex> guard(mutex_);
38 monitor_.notify_one();
43 std::lock_guard<std::mutex> guard(mutex_);
49 auto fut = p.get_future();
56 auto pr = std::promise<bool>();
57 auto fut = pr.get_future();
59 queue_.emplace_back(std::make_pair(std::move(f), std::move(pr)));
62 monitor_.notify_one();
67 WorkerThread::threadMain() {
71 FAISS_ASSERT(wantStop_);
73 for (
auto& f : queue_) {
75 f.second.set_value(
true);
80 WorkerThread::threadLoop() {
82 std::pair<std::function<void()>, std::promise<bool>> data;
85 std::unique_lock<std::mutex> lock(mutex_);
87 while (!wantStop_ && queue_.empty()) {
95 data = std::move(queue_.front());
100 data.second.set_value(
true);
void stop()
Request that the worker thread stop itself.
std::future< bool > add(std::function< void()> f)