如何在多线程中使用QTcpSocket? [英] how to use QTcpSocket in multithread?

查看:1906
本文介绍了如何在多线程中使用QTcpSocket?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最后一个问题中,我没有明确表达用例.现在我描述我的要求. 在我的客户端中,我需要使用QTcpSocket连接到服务器.在大多数情况下,我可以在gui线程(主线程)中进行读写. 但是,在一些耗时的任务中,我需要一个工作线程来执行任务.当我在gui线程中连接服务器时,我一直保持连接. 如果我在工作线程中使用新的QTcpSocket,是否必须在GUI线程中断开套接字?并再次连接到服务器?

in last question,i don't express my use-case clearly. now i describe my requirement. in my client,i need to connect to Server with QTcpSocket.in most cases, i can write and read in gui thread(main thread). how ever,in some time-consuming tasks,i need a single worker thread to execute task. when i connect the server in gui thread,i keep the connection all the time. if i use a new QTcpSocket in worker thread,must i disconnect the socket in gui thread? and connect to Server again?

class Widget:public QWidget
{
    Q_OBJECT
public:
    Widget()
    {
        socket = new QTcpSocket(this);
    }   
private:
    QHostAddress host;
    unsigned int port;
    QTcpSocket *socket;
private slots:  
    void startConnect()
    {
        socket->connectToHost(host,port);
    }
    void sendData()
    {
        //just send some bytes
        //socket->write();
    }
    void doTimeConsumingTask()
    {
        //must i disconnect gui->socket connection before the Worker is running?
        // socket->disconnectFromHost();
        Worker * w = new Worker();
        w->start();
    }
}

class Worker:public QThread
{
    Q_OBJECT
public:
    Worker()
    {

    }
protected:
    void run()
    {
        QTcpSocket socket;
        // must i connectToHost again here ? 


        // do something consuming time
        while(true){
            //
            QThread::msleep(10);

        }
    }
private:
    QHostAddress host;
    unsigned int port;
}

推荐答案

对您的问题的简单解决方案是不直接从工作线程访问QTcpSocket.最终,您可能想对工作线程中的套接字进行的所有操作都是向套接字发送数据和/或从套接字接收数据.在这种情况下,您可以简单地使工作线程发出带有QByteArray参数的信号,然后将该信号连接到GUI线程上的插槽,然后在QTcpSocket上调用write(const QByteArray& byteArray).同样,您可以使数据接收功能(无论连接到套接字的readyRead()信号如何)都驻留在工作线程中,也可以驻留在您的主线程中,但是在接收到工作线程需要处理的数据时,通过信号/插槽连接传递数据.

The easy solution to your problem is to not directly access the QTcpSocket from the worker thread. Ultimately, all you probably want to do with the socket from the worker thread is send and/or receive data to/from it. In that case, you can simply have the worker thread emit a signal with a QByteArray parameter, then connect that signal to a slot on the GUI thread that then calls write(const QByteArray& byteArray) on the QTcpSocket. Similarly, you can have your data receive function (whatever is connected to the readyRead() signal of the socket) either live in the worker thread, or live in your main thread, but upon receipt of data that the worker thread needs to process, pass it that data via a signal/slot connection.

另一种选择是,如果您必须直接访问套接字(例如,您需要做的不仅仅是写套接字,也许您必须能够关闭它或执行其他操作),则需要将该套接字的所有使用包装在互斥锁,在两个线程上.最好通过将原始QTcpSocket包装在另一个类中来完成此操作,该类仅公开您需要的一些方法,并且始终具有如下调用: QMutexLocker locker(theMutex); 在每个功能的开头.

The alternative, if you must directly access the socket (say, you need to do more than write to it, maybe you have to be able to close it or whatever), you need to wrap every use of that socket in a mutex, on both threads. This is probably best done by wrapping the raw QTcpSocket in another class that exposes only a few methods that you need, and always has a call like: QMutexLocker locker(theMutex); At the beginning of each function.

这篇关于如何在多线程中使用QTcpSocket?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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