C++ 使用std::mutex成员和运算符对std::of流成员进行线程安全访问<&书信电报;插入过载?
我在尝试创建一个包含std::ofstream和std::mutex的类时遇到了最糟糕的情况,std::mutex被锁定和解锁以控制对ofstream的访问 基本上,我想要一个流的类线程安全的,类似于的东西C++ 使用std::mutex成员和运算符对std::of流成员进行线程安全访问<&书信电报;插入过载?,c++,operator-overloading,ofstream,C++,Operator Overloading,Ofstream,我在尝试创建一个包含std::ofstream和std::mutex的类时遇到了最糟糕的情况,std::mutex被锁定和解锁以控制对ofstream的访问 基本上,我想要一个流的类线程安全的,类似于的东西 template<typename T> thread_safe_ofstream& operator<<(const T& thing) { std::lock_guard<std::mutex> guard(mu); s
template<typename T>
thread_safe_ofstream& operator<<(const T& thing)
{
std::lock_guard<std::mutex> guard(mu);
stream << thing;
return *this;
}
模板
stream&operator的thread_safe_您可以有这样的实现,例如:
class thread_safe_ofstream
{
std::mutex mu;
std::ofstream stream;
template<typename T>
void put(const T& thing) {
std::lock_guard<std::mutex> lock(mu);
stream << thing;
}
friend template<typename T>
thread_safe_ofstream& operator<<(thread_safe_ofstream& tsof, const T& value) {
tsof.put(value);
return tsof;
}
};
流的线程安全类
{
std::互斥mu;
std::流流流;
模板
无效认沽权(const T&thing){
标准:锁和防护锁(mu);
stream这里有一个完整的类,它将打印像ts_of s这样的命令中大部分未加密的文本,虽然出于某种原因它不能接受std::endl?我可以它让我很困惑,但是ofstream的任何“流畅”操作(我认为自定义运算符或标准流必须是线程安全的。你必须更具体地说明你想用这个锁实现什么。@PeteBecker,可能是从单个@SergeyA输出的-是的,当然可以猜测问题的意图。我在寻找更明确的东西,但两者都没有你我都不能提供。我不知道流必须是线程安全的。我试图排除一些错误,但它们似乎与线程无关,但我很难证明没有这个类。但也应该避免多行同时写入的交错问题。探索ion只是如何锁定多个线程正在使用的流,以便一次只有一个线程可以访问它。
template<typename T>
thread_safe_ofstream& operator<<(const T& thing)
{
std::lock_guard<std::mutex> guard(mu);
stream << thing;
return *this;
}
class thread_safe_ofstream
{
std::mutex mu;
std::ofstream stream;
template<typename T>
void put(const T& thing) {
std::lock_guard<std::mutex> lock(mu);
stream << thing;
}
friend template<typename T>
thread_safe_ofstream& operator<<(thread_safe_ofstream& tsof, const T& value) {
tsof.put(value);
return tsof;
}
};
struct ts_ofstream
{
std::ofstream ostream;
std::mutex mu;
//This is only for testing, you can initialize ostream however/wherever you like.
void init(bool test = false)
{
ostream = std::ofstream("C:/Users/Joey/Desktop/THREAD_DEBUG.txt", ios::out);
if (test)
{
ostream << "Testing stream ";
ostream << std::endl;
ostream << "Testing complete" << std::endl;
}
}
template <typename T>
friend ts_ofstream& operator<<(ts_ofstream& s, const T& x);
// function that takes a custom stream, and returns it
typedef ts_ofstream& (*MyStreamManipulator)(ts_ofstream&);
// take in a function with the custom signature
ts_ofstream& operator<<(MyStreamManipulator manip)
{
// call the function, and return it's value
return manip(*this);
}
// define the custom endl for this stream.
// note how it matches the `MyStreamManipulator`
// function signature
static ts_ofstream& endl(ts_ofstream& stream)
{
// print a new line
stream.ostream << std::endl;
return stream;
}
// this is the type of std::ofstream
typedef std::basic_ostream<char, std::char_traits<char> > CoutType;
// this is the function signature of std::endl
typedef CoutType& (*StandardEndLine)(CoutType&);
// define an operator<< to take in std::endl
ts_ofstream& operator<<(StandardEndLine manip)
{
// call the function, but we cannot return it's value
manip(this->ostream);
return *this;
}
};
template<typename T>
ts_ofstream & operator<<(ts_ofstream & s, const T & x)
{
std::lock_guard<std::mutex> lock(s.mu);
s.ostream << x;
return s;
}