改变线程的标准输入和输出

时间:2021-11-10 00:35:13

I have an application that creates two threads. (thread_1 for a Qt GUI and thread_2 for an app that runs a TCL interpreter).

我有一个创建两个线程的应用程序。 (用于Qt GUI的thread_1和用于运行TCL解释器的应用程序的thread_2)。

I want thread_1 (Qt GUI) to create a command and send it to thread_2 (TCL interpreter).

我想要thread_1(Qt GUI)创建命令并将其发送到thread_2(TCL解释器)。

I'm thinking of connecting thread_1's stdout to thread_2's stdin, and I don't know how to do it ? if you know how to do it or can suggest different way of work, I'd appreciate your help.

我正在考虑将thread_1的stdout连接到thread_2的stdin,我不知道该怎么做?如果你知道如何做或者可以提出不同的工作方式,我将非常感谢你的帮助。

2 个解决方案

#1


1  

The solution I propose requires to set up 2 std::queue<> or std::list to let each thread pass a message to the other one and vice versa. The simplest way is to have each thread setup its own incoming message queue, and let other threads get a pointer to it. First you need a synchronized version of the queue datatype: as I gave in the comment, there's an implementation there.

我建议的解决方案需要设置2 std :: queue <>或std :: list让每个线程将消息传递给另一个,反之亦然。最简单的方法是让每个线程都设置自己的传入消息队列,让其他线程获得指向它的指针。首先,您需要队列数据类型的同步版本:正如我在评论中给出的,那里有一个实现。

Then you only need to upgrade your thread class (or runnable class, or whatever you're using as an abstraction of a task) with one such queue available internally, and a send method publicly accessible so that other tasks may post a message to it. Your task will then have to periodically check that queue for incoming message, and eventually process it.

然后你只需要升级你的线程类(或者可运行的类,或者你用作任务的抽象的任何东西),内部有一个这样的队列,一个可公开访问的发送方法,以便其他任务可以向它发送消息。然后,您的任务必须定期检查该队列是否有传入消息,并最终处理它。

NB: I got that page from stack overflow itself, since the blog owner is a member of this community. See that page talking about queue synchronization issue.

注意:我从堆栈溢出本身获得了该页面,因为博客所有者是该社区的成员。请参阅该页面,讨论队列同步问题。

#2


0  

I am not sure why you would like to go through standard input and output here, but I think the issue might be much simpler than you think. I would just personally use the qt signal-slot mechanism as follows:

我不确定你为什么要在这里通过标准输入和输出,但我认为问题可能比你想象的要简单得多。我个人会使用qt信号槽机制,如下所示:

connect(guiThreadSender, SIGNAL(sendCommand(const QByteArray&)),
        tclThreadReceiver, SLOT(handleCommand(const QByteArray&)));

#1


1  

The solution I propose requires to set up 2 std::queue<> or std::list to let each thread pass a message to the other one and vice versa. The simplest way is to have each thread setup its own incoming message queue, and let other threads get a pointer to it. First you need a synchronized version of the queue datatype: as I gave in the comment, there's an implementation there.

我建议的解决方案需要设置2 std :: queue <>或std :: list让每个线程将消息传递给另一个,反之亦然。最简单的方法是让每个线程都设置自己的传入消息队列,让其他线程获得指向它的指针。首先,您需要队列数据类型的同步版本:正如我在评论中给出的,那里有一个实现。

Then you only need to upgrade your thread class (or runnable class, or whatever you're using as an abstraction of a task) with one such queue available internally, and a send method publicly accessible so that other tasks may post a message to it. Your task will then have to periodically check that queue for incoming message, and eventually process it.

然后你只需要升级你的线程类(或者可运行的类,或者你用作任务的抽象的任何东西),内部有一个这样的队列,一个可公开访问的发送方法,以便其他任务可以向它发送消息。然后,您的任务必须定期检查该队列是否有传入消息,并最终处理它。

NB: I got that page from stack overflow itself, since the blog owner is a member of this community. See that page talking about queue synchronization issue.

注意:我从堆栈溢出本身获得了该页面,因为博客所有者是该社区的成员。请参阅该页面,讨论队列同步问题。

#2


0  

I am not sure why you would like to go through standard input and output here, but I think the issue might be much simpler than you think. I would just personally use the qt signal-slot mechanism as follows:

我不确定你为什么要在这里通过标准输入和输出,但我认为问题可能比你想象的要简单得多。我个人会使用qt信号槽机制,如下所示:

connect(guiThreadSender, SIGNAL(sendCommand(const QByteArray&)),
        tclThreadReceiver, SLOT(handleCommand(const QByteArray&)));