ThreadSafeOutputFileStream

Macros

Line Code
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
#ifndef FWCore_Concurrency_ThreadSafeOutputFileStream_h
#define FWCore_Concurrency_ThreadSafeOutputFileStream_h

#include "oneapi/tbb/concurrent_queue.h"

#include <atomic>
#include <fstream>
#include <string>

namespace edm {
  class ThreadSafeOutputFileStream {
  public:
    ThreadSafeOutputFileStream(std::string const& name);
    ~ThreadSafeOutputFileStream();

    void write(std::string&& msg);
    explicit operator bool() const { return static_cast<bool>(file_); }

  private:
    std::ofstream file_;
    std::atomic<bool> msgBeingLogged_{false};
    oneapi::tbb::concurrent_queue<std::string> waitingMessages_{};
  };
}  // namespace edm

#endif