1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-08 19:46:02 +01:00

ThreadPool::enqueue(): Use move semantics

This avoids a superfluous copy of the work item.
This commit is contained in:
Eelco Dolstra 2025-11-03 13:58:23 +01:00
parent 5e025ce940
commit 4a0ccc89d9
2 changed files with 3 additions and 3 deletions

View file

@ -36,7 +36,7 @@ public:
/** /**
* Enqueue a function to be executed by the thread pool. * Enqueue a function to be executed by the thread pool.
*/ */
void enqueue(const work_t & t); void enqueue(work_t t);
/** /**
* Execute work items until the queue is empty. * Execute work items until the queue is empty.

View file

@ -41,12 +41,12 @@ void ThreadPool::shutdown()
thr.join(); thr.join();
} }
void ThreadPool::enqueue(const work_t & t) void ThreadPool::enqueue(work_t t)
{ {
auto state(state_.lock()); auto state(state_.lock());
if (quit) if (quit)
throw ThreadPoolShutDown("cannot enqueue a work item while the thread pool is shutting down"); throw ThreadPoolShutDown("cannot enqueue a work item while the thread pool is shutting down");
state->pending.push(t); state->pending.push(std::move(t));
/* Note: process() also executes items, so count it as a worker. */ /* Note: process() also executes items, so count it as a worker. */
if (state->pending.size() > state->workers.size() + 1 && state->workers.size() + 1 < maxThreads) if (state->pending.size() > state->workers.size() + 1 && state->workers.size() + 1 < maxThreads)
state->workers.emplace_back(&ThreadPool::doWork, this, false); state->workers.emplace_back(&ThreadPool::doWork, this, false);