mirror of
https://github.com/MartinOpat/cuda-based-raytrace.git
synced 2025-06-07 02:13:10 +02:00
buffer multiple files
This commit is contained in:
49
src/hurricanedata/worker.cpp
Normal file
49
src/hurricanedata/worker.cpp
Normal 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