我一直在四处寻找各种方法来解决线程安全日志问题,但我还没有看到任何相当类似的东西,所以我不知道是否因为作为C++的一个完整的新手而没有注意到它是某种可怕的东西,线程和iostreams。它似乎在我已经完成的基本测试中起作用。我的线程安全日志类的方法很糟糕吗?
基本上我有一个Log类(创意,我知道...),它具有为标准操纵器设置的操作符< <,所以我可以快活地传入任何我想要的东西。
但是,我知道是这样的:
std::cout << "Threads" << " will" << " mess" << " with" << "this." << std::endl;
将有可能得到交错当多个线程编写来清点(或其他地方登录的ostream点)。所以,我创建了一些特定于日志类操纵,让我做到这一点:
Log::log << lock << "Write" << " what" << " I" << " want" << std::endl << unlock;
我只是想知道,如果这是一个固有的可怕想法,记住,我愿意接受轴承用户的Log类需要通过'锁定'和'解锁'进行处理。我认为使'std :: endl'自动解锁,但这似乎会造成更多的麻烦......我认为散布的使用应该在测试中出现,但是如果任何人都可以看到一种方法来进行编译,时间错误,那很好。
我也很感激任何建议,使我的代码更清洁。
以下是该课堂的简化版本,用于演示目的;整个事情有更多的构造函数采用类似文件名的东西,所以与问题无关。
#include <iostream>
#include <thread>
#include <fstream>
class Log{
public:
//Constructors
Log(std::ostream & os);
// Destructor
~Log();
// Input Functions
Log & operator<<(const std::string & msg);
Log & operator<<(const int & msg);
Log & operator<<(std::ostream & (*man)(std::ostream &)); // Handles manipulators like endl.
Log & operator<<(std::ios_base & (*man)(std::ios_base &)); // Handles manipulators like hex.
Log & operator<<(Log & (*man)(Log &)); // Handles custom Log manipulators like lock and unlock.
friend Log & lock(Log & log); // Locks the Log for threadsafe output.
friend Log & unlock(Log & log); // Unlocks the Log once threadsafe output is complete.
private:
std::fstream logFile;
std::ostream & logStream;
std::mutex guard;
};
// Log class manipulators.
Log & lock(Log & log); // Locks the Log for threadsafe output.
Log & unlock(Log & log); // Unlocks the Log once threadsafe output is complete.
void threadUnsafeTask(int * input, Log * log);
void threadSafeTask(int * input, Log * log);
int main(){
int one(1), two(2);
Log log(std::cout);
std::thread first(threadUnsafeTask, &one, &log);
std::thread second(threadUnsafeTask, &two, &log);
first.join();
second.join();
std::thread third(threadSafeTask, &one, &log);
std::thread fourth(threadSafeTask, &two, &log);
third.join();
fourth.join();
return 0;
}
void threadUnsafeTask(int * input, Log * log){
*log << "Executing" << " thread '" << *input << "', " << "expecting " << "interruptions " << "frequently." << std::endl;
}
void threadSafeTask(int * input, Log * log){
*log << lock << "Executing" << " thread '" << *input << "', " << "not expecting " << "interruptions." << std::endl << unlock;
}
// Constructors (Most left out as irrelevant)
Log::Log(std::ostream & os): logFile(), logStream(logFile), guard(){
logStream.rdbuf(os.rdbuf());
}
// Destructor
Log::~Log(){
logFile.close();
}
// Output Operators
Log & Log::operator<<(const std::string & msg){
logStream << msg;
return *this;
}
Log & Log::operator<<(const int & msg){
logStream << msg;
return *this;
}
Log & Log::operator<<(std::ostream & (*man)(std::ostream &)){
logStream << man;
return *this;
}
Log & Log::operator<<(std::ios_base & (*man)(std::ios_base &)){
logStream << man;
return *this;
}
Log & Log::operator<<(Log & (*man)(Log &)){
man(*this);
return *this;
}
// Manipulator functions.
Log & lock(Log & log){
log.guard.lock();
return log;
}
Log & unlock(Log & log){
log.guard.unlock();
return log;
}
它为我在Ubuntu12.04克++编译:
g++ LogThreadTest.cpp -o log -std=c++0x -lpthread
相关,以使定制操纵是无耻地从here但那儿剽窃不要怪他们我的无能copypasta位。
这是一个错误在这里不使用RAII。 –
恕我直言,一个无锁FIFO将是一个更好的主意在这里..以某种方式线程暂停记录不吸引力。 – vrdhn
依靠用户锁定和解锁东西是不可靠的。一种选择是让您的日志文件同时从用户那里获取消息,并通过将它们放入单个队列并在单独的线程中运行它们来序列化它们。所以从多用户的角度来看,这些呼叫是非阻塞的,但实际上没有交织。参见[Herb Sutter的这篇演讲](http://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2012-Herb-Sutter-Concurrency-and-Parallelism)了解更多信息。我经历了实现他的并发对象包装的工作版本的练习。 – juanchopanza