1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#pragma once
#include "image.h"
#include <thread>
#include <mutex>
#include "logger.h"
#include "io.h"
namespace image
{
class ImageSavingThread
{
private:
std::thread saving_thread;
std::vector<std::pair<Image, std::string>> queue;
std::mutex queue_mutex;
bool thread_should_run = true;
void actual_saving_thread()
{
while (thread_should_run || queue.size() > 0)
{
queue_mutex.lock();
bool has_images = queue.size() > 0;
queue_mutex.unlock();
if (has_images)
{
queue_mutex.lock();
auto img = queue[0];
queue.erase(queue.begin());
queue_mutex.unlock();
// logger->debug("Queue length %d", queue.size());
logger->info("Saving " + img.second);
save_img(img.first, img.second);
continue;
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
public:
bool use_thread;
public:
ImageSavingThread(bool use_thread) : use_thread(use_thread)
{
if (use_thread)
saving_thread = std::thread(&ImageSavingThread::actual_saving_thread, this);
}
~ImageSavingThread()
{
thread_should_run = false;
if (use_thread && saving_thread.joinable())
saving_thread.join();
}
void push(Image &img, std::string path)
{
if (use_thread)
{
queue_mutex.lock();
queue.push_back({img, path});
queue_mutex.unlock();
}
else
{
logger->info("Saving " + path);
save_img(img, path);
}
}
};
}
|