是qDebug()线程安全吗? [英] Is qDebug() thread-safe?

查看:522
本文介绍了是qDebug()线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

qDebug()线程安全吗?通过线程安全的,我不只是指未崩溃,而且如果我叫 qDebug()来自不同线程,是有可能的输出成为混合起来?我使用此代码来测试的,它不会出现如此,但是,我不能在那里他们谈论这个文档中的任何地方。

Is qDebug() thread-safe? By thread-safe I don't just mean not-crashing, but also if I call qDebug() from different threads, is it possible for the output to become mixed-up? I tested it with this code, and it doesn't appear to be so, however, I couldn't find anywhere in the documentation where they talk about this.

这是我的测试代码:

#include <QtConcurrent>
#include <QApplication>
void print_a() {
    for (int ii = 0; ii < 10000; ii++) {
        qDebug("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
    }
}
void print_b()
{
    for (int ii = 0; ii < 10000; ii++) {
        qDebug("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
    }
}
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QtConcurrent::run(print_a);
    QtConcurrent::run(print_b);
    return a.exec();
}

没有'a'和'b'同一行的任何地方,但我仍然不知道,如果是100%线程安全的...

There were no 'a' and 'b' mixed in the same line anywhere, but I'm still not sure if it's 100% thread safe...

推荐答案

继是我的回答和评论:


  1. 如果qDebug的文档()并没有提到是否是线程安全与否,我们应该假设它不是。答案可能是依赖于平台的:如何qDebug()在系统级(Linux操作系统,窗户,...)被实现

  1. If the documentation of qDebug() does not mention whether it is thread-safe or not, we should assume it is not. The answer is likely platform-dependent: how qDebug() is implemented at the system level (Linux, Windows, ...).

相反的更宽线程安全问题,我想你问一个更具体的问题,像这样:在多线程应用程序中使用qDebug()是否会导致交错输出线?答案是是的,偶尔。正如@dmcontador上面的结果所证明的那样。

Instead of the broader question of thread-safety, I think you were asking a more specific question like this: "Will the use of qDebug() in a multi-threaded application lead to interleaved output lines?" The answer is "Yes, occasionally." as demonstrated by the results produced by @dmcontador above. And the probability increases when the strings to be printed out are getting longer, as explained by @quetzalcoatl above.

答案不取决于你是否使用qDebug (...)或qDebug()<< ...,因为两者最终都会调用系统级实现代码。

The answer does not depend on whether you use qDebug("...") or qDebug() << "...", as both will finally call the system-level implementation code.

示例代码。所以我创建了一个新的例子如下所示:

It is not easy for me to produce interleaved output lines using your original example code. So I have created a new example as shown below:

#include <QCoreApplication>
#include <QtConcurrent>

#define MAX_ITERS 10
#define MAX_LEN   10000

void print_a()
{
    QString a(MAX_LEN, 'a');

    for(int i = 0; i < MAX_ITERS; ++i) {
        qDebug().noquote() << a;
    }
}

void print_b()
{
    QString b(MAX_LEN, 'b');

    for(int i = 0; i < MAX_ITERS; ++i) {
        qDebug().noquote() << b;
    }
}

int main(int argc, char * argv[])
{
    QCoreApplication a(argc, argv);
    QtConcurrent::run(print_a);
    QtConcurrent::run(print_b);
    return 0;
}


您将增加MAX_LEN。

The probability increases when you increase MAX_LEN.


  1. 后续问题是:如何使用qDebug交织输出线?一个解决方案是在每个qDebug()行使用QMutex。请注意,我没有尝试过这种不切实际的解决方案。

这篇关于是qDebug()线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆