diff --git a/src/OutputFileHandler.cc b/src/OutputFileHandler.cc index 773c7cfb3db872f600dc61deaabdf8c80b0e845b..09655c8381a80945c6667572877f0cd8265eeefe 100644 --- a/src/OutputFileHandler.cc +++ b/src/OutputFileHandler.cc @@ -11,7 +11,7 @@ const std::string OutputFileHandler::journal_file_ = "index.journal"; tbb::concurrent_bounded_queue<std::pair<std::string, FILE *>> OutputFileHandler::files_to_close_ = tbb::concurrent_bounded_queue<std::pair<std::string, FILE *>>(); -bool OutputFileHandler::file_handler_running_ = true; +std::atomic<bool> OutputFileHandler::file_handler_running_{true}; void OutputFileHandler::enqueue_current_file_for_close_and_move_maybe() { if (current_file_ != 0) { @@ -86,7 +86,11 @@ void OutputFileHandler::close_and_rename::operator()() const { // std::cout << std::this_thread::get_id() << " try pop" << // files_to_close_.size() << std::endl; LOG(TRACE) << "try pop now. queue size now " << files_to_close_.size(); - files_to_close_.pop(n); + try { + files_to_close_.pop(n); + } catch (tbb::user_abort &e) { + break; + } LOG(TRACE) << "popping file: " << n.first << " for closing, queue size now " << files_to_close_.size(); if (fclose(n.second) != 0) { diff --git a/src/OutputFileHandler.h b/src/OutputFileHandler.h index 08a55cf438ce3ed49a56e8cece9316cb10ef0d3d..06fc457fbacab10f93489d2189516f7c53ce1290 100644 --- a/src/OutputFileHandler.h +++ b/src/OutputFileHandler.h @@ -1,6 +1,7 @@ #ifndef OUTPUTFILEHANDLER_H #define OUTPUTFILEHANDLER_H +#include <atomic> #include <cstdint> #include <cstdio> #include <string> @@ -21,8 +22,8 @@ class OutputFileHandler { current_file_(0), close_and_rename_(base_path_), t{} { - t = std::thread(close_and_rename_); create_output_directory_maybe(working_files_base_path_); + t = std::thread(close_and_rename_); } ~OutputFileHandler() { @@ -44,7 +45,8 @@ class OutputFileHandler { std::string format_filename(uint32_t run_number, uint32_t index); void exit_file_handler_loop() { - file_handler_running_ = false; + file_handler_running_.store(false, std::memory_order_release); + files_to_close_.abort(); t.join(); } @@ -66,7 +68,8 @@ class OutputFileHandler { static const std::string journal_file_; static tbb::concurrent_bounded_queue<std::pair<std::string, FILE *>> files_to_close_; - static bool file_handler_running_; + + static std::atomic<bool> file_handler_running_; std::string base_path_; std::string filename_prefix_;