buffer multiple files

This commit is contained in:
Robin committed 2025-01-08 09:49:29 +01:00
1 parent dbebb6d9f2
commit be0659c06f
5 files changed
+149 -98

No files matched your search

+49
View File
@@ -0,0 +1,49 @@
#include "worker.h"
#include <iostream>
using namespace std;
Worker::Worker(): workerRunning(false) {
ioworker = thread([this]() { jobRunner(); });
}
void Worker::enqueueJob(Job job) {
{
lock_guard<mutex> lk(queuecv_m);
jobs.push(move(job));
}
queuecv.notify_all();
}
void Worker::jobRunner() {
workerRunning = true;
while(workerRunning) {
Job job;
{
unique_lock<mutex> lk(queuecv_m);
queuecv.wait(lk, [this]{ return !workerRunning || !jobs.empty(); });
if (!workerRunning) {
return;
}
job = move(jobs.front());
jobs.pop();
}
job();
}
}
void Worker::stop() {
if (workerRunning == false) return;
{
lock_guard<mutex> lk(queuecv_m);
workerRunning = false;
}
queuecv.notify_all();
ioworker.join();
}
Worker::~Worker() {
stop();
}
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.