Qt 中的高速数据记录

问题描述 投票:0回答:3

我有一个应用程序,它将以高达 ~50MB/s 的速度接受数据。我当前的设置是让一个线程从网络接收数据并将其放入另一个线程中的 QQueue 中。然后将该 QQueue 清空到文件中。此设置似乎在四核 2.5GHz i7 上运行良好,但在 8 核 1.8GHz Xeon 上运行不佳。两者都有相似数量的 RAM。 Xeon 上的问题是在网络上传输的数据和写入文件之间存在数据丢失。我很清楚数据丢失是由于我从网络接收数据的能力造成的。我必须使用 QUdpSocket 来接收数据,重要的是我接收所有数据,但不一定按顺序。有谁知道如何提高我怀疑是问题的单线程性能?

void ReceiveThread::readPendingDatagrams() {
    // Process data while socket still has pending datagrams
    while( udpReceiveSocket->hasPendingDatagrams() ) {
        data = new QByteArray;
        // Update the size
        size = udpReceiveSocket->pendingDatagramSize();
        // Reserve the size in the byte array
        data->reserve( size );
        // Store data into qbytearray
        udpReceiveSocket->readDatagram( data->data(), size, &sender,
                                        &senderPort );
        // Send the data to the file thread
        fileWriteThread.addPacketToQueue( data );
    }
}


void FileWriteThread::addPacketToQueue(QByteArray * data) {
    // Lock the mutex
    QMutexLocker locker(&mutex);
    // Add the data packet to the queue
    dataQueue.enqueue( data );

    numReceieved++;
    locker.unlock();
    // Wake the thread that is waiting for this
    dataAdded.wakeOne();
}

void FileWriteThread::run() {    
    forever {
        // Lock the mutex
        QMutexLocker locker(&mutex);
        // Check if the packet queue is empty
        while( dataQueue.isEmpty() ) {
            // Sleep the thread
            dataAdded.wait(&mutex);
        }
        // Get the data from the queue
        data = dataQueue.dequeue();
        locker.unlock();
        // Write the data to the file
        writeDataToBuffer(data);
    }
}
c++ multithreading qt
3个回答
1
投票

避免重复致电新接线员并预订。两者都可能非常慢。尝试缓存并重用对象。顺便说一句,你在哪里删除使用 new 分配的对象?


0
投票

有点晚了;-)。但也许您应该考虑使套接字接收缓冲区更大。这不是 QT 缓冲区,而是较低级别的缓冲区。从 QT 5.3 开始,可以使用函数 setSocketOption、枚举 QAbstractSocket::ReceiveBufferSizeSocketOption 设置。


0
投票

如何使用 10Gbps 以太网链路将数据存储到文件中。

© www.soinside.com 2019 - 2024. All rights reserved.