我一直在寻找解决线程安全日志记录问题的各种方法,但我还没有看到像这样的东西,所以我不知道它是否有点糟糕,因为我是 C++ 的一个完整的新手,线程和 iostream。它似乎适用于我已经完成的基本测试。
基本上我有一个 Log 类(创意,我知道......),它为标准操纵器设置了 operator<<,所以我可以愉快地传递我想要的任何东西。
但是,我知道类似:
std::cout << "Threads" << " will" << " mess" << " with" << "this." << std::endl;
当多个线程写入 cout (或 Log ostream 指向的任何地方)时,可能会交错。因此,我创建了一些特定于 Log 类的操纵器,让我可以这样做:
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;
}
它适用于我在 Ubuntu 12.04 g++ 上,编译为:
g++ LogThreadTest.cpp -o log -std=c++0x -lpthread
与制作定制机械手相关的部分从这里被无耻地抄袭,但不要因为我无能的复制面而责怪他们。