forked from mothur/mothur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsharedwriter.hpp
executable file
·39 lines (31 loc) · 1.12 KB
/
sharedwriter.hpp
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
//
// SharedWriter.hpp
// Mothur
//
// Created by Sarah Westcott on 12/7/17.
// Copyright © 2017 Schloss Lab. All rights reserved.
//
#ifndef SharedWriter_hpp
#define SharedWriter_hpp
#include "mothurout.h"
#include "utils.hpp"
/***********************************************************************/
class SynchronizedOutputFile {
public:
SynchronizedOutputFile (const string& p) : path(p) { util.openOutputFile(p, out); }
SynchronizedOutputFile (const string& p, bool append) : path(p) { util.openOutputFileAppend(p, out); }
~SynchronizedOutputFile() { out.close(); }
void write (const string& dataToWrite) {
std::lock_guard<std::mutex> lock((writerMutex)); // Ensure that only one thread can execute at a time
out << dataToWrite;
}
void setFixedShowPoint() { out.setf(ios::fixed, ios::showpoint); }
void setPrecision(int p) { out << setprecision(p); }
private:
string path;
std::mutex writerMutex;
Utils util;
ofstream out;
};
/***********************************************************************/
#endif