[英]Which are thread safe printing syntaxes in C++, and Qt?
qDebug()
和std::cout
被认为不是线程安全的,因为他们的文档未提及任何有关线程安全的内容。
C ++和Qt中哪些是线程安全打印语法?
写入之前,请使用可锁定互斥锁的输出方法。 这样一来,一次只能写入一个线程。
#include <iostream>
#include <ostream>
#include <sstream>
#include <mutex>
class Logger {
public:
// destructor is called when output is collected and
// the temporary Logger() dies (after the ";").
~Logger() {
Logger::mutex_.lock(); // make sure I am the only one writing
std::cout << buffer_.str(); // write
std::cout << std::flush; // push to output
Logger::mutex_.unlock(); // I am done, now other threads can write
}
// funny construct you need to chain logs with "<<" like
// in std::cout << "var = " << var;
template <typename T>
Logger &operator<<(T input) {
buffer_ << input;
return *this;
}
private:
// collect the output from chained << calls
std::ostringstream buffer_;
// mutex shared between all instances of Logger
static std::mutex mutex_;
};
// define the static variable. This line must be in one .cpp file
std::mutex Logger::mutex_;
int main()
{
Logger() << "Hello" << " " << "World\n";
Logger() << 123 << "test\n";
return 0;
}
从它们不会导致程序崩溃的意义上来说,它们都是线程安全的。 同样,从所有发送到流的字符将被打印到其有意值的意义上说,输出也是安全的
线程A发送:{a,b,c,d}
线程B发送:{1,2,3,4}
有效的输出可以是{a,b,1,2,3,c,d,4},但是您永远不会看到不是来自该集合的字符,例如{m}。
为了序列化您的输出,您应该至少通过某种全局可访问的机制使用互斥。
例如,一个简单的课程就可以做到
class printsafe{
private:
typedef std::mutex t_lock;
static t_lock l_print;
public:
static void print(std::string const & s}
{
std::lock_guard<printsafe::t_lock> l;
std::cout << s ; // or qDebug(s.c_str());
}
};
现在您的输出将是{abcd1234}或{1234abcd},因为
线程A发送:abcd
线程B发送:1234
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.