简体   繁体   English

Java和C ++ / Qt之间的线程差异

[英]Difference in threads between in Java and in C++/Qt

The java version works exactly as expected while the C++ version crashes. 当C ++版本崩溃时,Java版本完全可以按预期运行。 The C++ version somehow does work when 1 <= N_THREADS <= 2 but otherwise it fails. 当1 <= N_THREADS <= 2时,C ++版本会以某种方式工作,但否则会失败。

I've struggled for hours trying to make the C++ version work but failed and still have no idea what is going on. 我努力使C ++版本正常工作了几个小时,但失败了,仍然不知道发生了什么。 For sure I will know the problem by studying system threads and the Qthread source code, and yes I will try to do that, but I also do need a solution now. 可以肯定的是,通过研究系统线程和Qthread源代码,我会知道问题所在,是的,我会尝试这样做,但是现在我也确实需要解决方案。

Any help appreciated. 任何帮助表示赞赏。

thread_test.java thread_test.java

class my_thread extends Thread
{
    int n;

    my_thread()
    {
        n = 0;
    }

    public void run()
    {
        try
        {
            while (n < 10)
            {
                System.out.print(n++);
                Thread.sleep(1000);
            }
        }
        catch (InterruptedException e)
        {
            return;
        }
    }
}

public class thread_test
{
    public static void main(String[] args)
    {
        final int N_THREADS = 10;
        for (int i = 0; i < N_THREADS; i++)
        {
            new my_thread().start();
        }
    }
}

thread_test.cpp thread_test.cpp

#include <array>
#include <QTextStream>
#include <QThread>

QTextStream qout(stdout);

class my_thread : public QThread
{
public:
    int n;

    my_thread()
    {
        n = 0;
    }

    void run()
    {
        while(n < 10)
        {
            qout << n++ << flush;
            msleep(1000);
        }
    }
};

int main()
{
    enum { N_THREADS = 10 };
    std::array<my_thread, N_THREADS> thread_array;
    for (auto& thread : thread_array)
    {
        thread.start();
    }
    for (auto& thread : thread_array)
    {
        thread.wait();
    }
    return 0;
}

EDIT 编辑

Reading the comments and with some Google search, the problem in my C++ code definitely seems to be using non-threadsafe operations in multiple threads. 阅读注释并通过一些Google搜索,我的C ++代码中的问题肯定似乎是在多个线程中使用了非线程安全的操作。 So here my problem will be solved by making QTextStream threadsafe just as Java's PrintStream. 因此,在这里,我的问题将通过使QTextStream像Java的PrintStream一样是线程安全的来解决。

Based on my quick web search I kind of guess that the process where QTextStream is interacting with the standard output shouldn't be done through multiple threads at the same time since there is only one standard output. 基于我的快速网络搜索,我猜测QTextStream与标准输出交互的过程不应同时通过多个线程进行,因为只有一个标准输出。 But it would be too slow to make all other threads wait during one thread is interacting with the standard output. 但是,如果一个线程正在与标准输出交互时,让所有其他线程等待,将太慢。 So I think the right solution would be making a queue to store the output to be written, as QString references, while stdout is busy and cannot accept more task at the same time. 因此,我认为正确的解决方案是使队列存储要写入的输出(作为QString引用),而stdout忙并且不能同时接受更多任务。 Well, then here how do I guarantee that this queue is thread safe? 好吧,那我如何保证这个队列是线程安全的呢? I am very confused right now. 我现在很困惑。 Also, how is Java PrintStream implemented as threadsafe? 此外,如何将Java PrintStream实现为线程安全的? Please help me organizing these issues and finding the right solution to this problem. 请帮助我组织这些问题并找到正确的解决方案。

In Qt you are not supposed to derive QThread because you want functionality run in a thread and not the thread having that functionality . Qt您不应该派生QThread因为您希望functionality run in a thread而不是在thread having that functionalitythread having that functionality

just have a look at those links, the first discribes the threading more general, the second helps implementing ... 看看这些链接,第一个描述更通用的线程,第二个帮助实现...

http://blog.qt.digia.com/blog/2010/06/17/youre-doing-it-wrong/ http://blog.qt.digia.com/blog/2010/06/17/youre-doing-it-wrong/

what is the correct way to implement a QThread... (example please...) 什么是实现QThread的正确方法...(请提供示例...)

cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM