Skip to content

Instantly share code, notes, and snippets.

Last active April 5, 2022 16:27
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
#include <vector>
#include <fstream>
#include <string>
#include <map>
#include <memory>
#include <chrono>
#include <pthread.h>
#include <thread>
#include <algorithm>
#include "./PrintableObject.h"
namespace FM
// a JobVector is a vector that defines the jobs (i.e. the objects to be written)
// sometimes you get tired of typing std::vector<std::shared_ptr<PrintableObject>> and managing the carats
using JobVector = std::vector<std::shared_ptr<PrintableObject>>;
// Makes it easier to see where runtime errors are coming from
class FileManagerException : public std::runtime_error
~FileManagerException() {}
FileManagerException(const std::string msg) : runtime_error(msg) {}
class FileManager
FileManager(bool append=false, int wait_timeout=120) : _append(append), _max_stop_wait(wait_timeout) {}
void stop(); // wraps up the queues and stops the manager PID
void start(); // starts the manager PID
void initFile(std::string filename); // creates entries for the filename in the maps
void queueJob(std::string filename, JobVector job);
std::map<std::string, std::shared_ptr<std::ofstream>> _filename_to_filehandle;
std::map<std::string, std::vector<JobVector>> _filename_to_jobs; // store jobs for each filename
std::vector<std::string> _filenames;
pthread_t _manager_thread_handle;
bool _append;
int _max_stop_wait; // Not yet implemented
void _manager(); // thread used to manage the queues
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment