[英]c++: Writing to a file in multithreaded program
因此,我有多个线程通过调用Log :: write方法写入同一文件。
class Log
{
private:
ofstream log;
string file_path;
public:
Log(string);
void write(string);
};
Log::Log(string _file_path)
{
file_path=_file_path;
}
void Log::write(string str)
{
EnterCriticalSection(&CriticalSection);
log.open(file_path.c_str(),std::ofstream::app);
log<<str+'\n';
log.close();
LeaveCriticalSection(&CriticalSection);
}
如果线程将同时调用同一对象的Log :: write方法是否安全?
您的代码很浪费,并且没有遵循C ++习惯用法。
从头开始:是的, write
是线程安全的,因为win32 CRITICAL_SECTION
可以防止它被并发修改。
虽然:
为什么每次都要打开和关闭流? 这是非常浪费的事情。 在构造函数中打开流并使其保持打开状态。 析构函数将处理关闭流。
如果要使用Win32关键部分, 至少要使其RAII安全。 创建一个包装对关键部分的引用的类,将其锁定在构造函数中,并在析构函数中对其进行解锁。 即使抛出异常,也可以通过这种方式-您可以确保锁定将被解锁。
反正CriticalSection
的减速在哪里? 它应该是Log
的成员。
您知道std::mutex
吗?
为什么要按值传递字符串? 这是非常低效的。 然后通过const引用传递。
您对某些变量( file_path
)使用snake_case,对其他变量( CriticalSection
)使用大驼峰式。 使用相同的约定。
str
永远不是字符串变量的好名字,文件流也不是日志。 是进行实际记录的东西。 logger
是一个更好的名字。 在我的更正中仅将其命名为m_file_stream
。
更正的代码:
class Log
{
private:
std::mutex m_lock;
std::ofstream m_file_stream;
std::string m_file_path;
public:
Log(const std::string& file_path);
void write(const std::string& log);
};
Log::Log(const std::string& file_path):
m_file_path(file_path)
{
m_file_stream.open(m_file_path.c_str());
if (!m_file_stream.is_open() || !m_file_stream.good())
{
//throw relevant exception.
}
}
void Log::write(const std::string& log)
{
std::lock_guard<std::mutex> lock(m_lock);
m_file_stream << log << '\n';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.