12 #include "WorkerThread.h"
13 #include "../../FaissAssert.h"
15 namespace faiss {
namespace gpu {
17 WorkerThread::WorkerThread() :
25 WorkerThread::~WorkerThread() {
31 WorkerThread::startThread() {
32 thread_ = std::thread([
this](){ threadMain(); });
36 WorkerThread::stop() {
37 std::lock_guard<std::mutex> guard(mutex_);
40 monitor_.notify_one();
44 WorkerThread::add(std::function<
void()> f) {
45 std::lock_guard<std::mutex> guard(mutex_);
51 auto fut = p.get_future();
58 auto pr = std::promise<bool>();
59 auto fut = pr.get_future();
61 queue_.emplace_back(std::make_pair(std::move(f), std::move(pr)));
64 monitor_.notify_one();
69 WorkerThread::threadMain() {
73 FAISS_ASSERT(wantStop_);
75 for (
auto& f : queue_) {
77 f.second.set_value(
true);
82 WorkerThread::threadLoop() {
84 std::pair<std::function<void()>, std::promise<bool>> data;
87 std::unique_lock<std::mutex> lock(mutex_);
89 while (!wantStop_ && queue_.empty()) {
97 data = std::move(queue_.front());
102 data.second.set_value(
true);
107 WorkerThread::waitForThreadExit() {