PDA

View Full Version : workerThread calling static QVector - will freeze if not running for many hours



swankster
28th August 2017, 15:07
I have a application that parses a UDP stream. Then monitor that stream for a trigger event to start my thread. within that thread is a worker thread that calls a function which contains a static QVector. On every trigger event I initiate a vector.swap and vector.resize/reserve (have used both with same results).

with the program running and continually receiving UDP data. It runs beautifully with not issues. This data is then written to MySQL. a second thread is also running and writing reference data to another table in MySQL. Both MySQL write functions are on separate connections.

However if my program is left running overnight with no UPD stream. My program will hang and must be re-started to work again. This is not the case with 4 hours of no UDP. I have not determined a specific time as of yet. Not sure if my issue id with my Thread or my Vector. The 2nd thread function will pick up and continue to run normally.

Below are a few code snippets.

System Windows 7 Prof.
creator 4.1.0
Qt 5.6.2(Gcc 6.1.0, 64 bit)
using C++11



[Main Thread]
{
frmThread = new QThread;
workerThread = new StreamToVector();
workerThread->moveToThread(frmThread);
connect(workerThread, &DataStreamToVector::destroyed, frmThread, &QThread::quit);
connect(workerThread, &DataStreamToVector::destroyed, workerThread, &DataStreamToVector::deleteLater);
connect(this, &DataStreamToBuffer::DataToVector_Signal, workerThread, &DataStreamToVector::parseStreamToVector_Slot, Qt::DirectConnection);
frmThread->start();

DataToVector_Signal(dataStream, dataSize, RecordsPerTriggerEvent);
}

[worker thread .h ]
QThread *frmThread;

[worker thread .cpp ]
void DataStreamToVector::parseStreamToVector_Slot(QStri ng dataStream, int dataSize, uint RecordsPerTriggerEvent)
{
static QVector < QVector<QString> > cycleBuffer(63, QVector<QString>(MAX_CYCLE_SIZE_x10MS));
static uint recordCnt = 1;

if ( recordCnt > RecordsPerTriggerEvent ) {
QVector < QVector<QString> > defaultBuffer(63, QVector<QString>(MAX_CYCLE_SIZE_x10MS));
recordCnt = 1;
cycleBuffer.swap(defaultBuffer);
cycleBuffer.reserve(GALIL_40x0_ADDRESS_COUNT);
}

if ( recordCnt <= recPerCycle ) {
cycleBuffer[0][recordCnt].append(dataStream.mid( 12,2 )); // general Input Block 0 address
cycleBuffer[1][recordCnt].append(dataStream.mid( 14,2 ));
// recordCnt typically 350 - 800

if ( recordCnt == RecordsPerTriggerEvent )
writeRecordsToDB(RecordsPerTriggerEvent, cycleBuffer);

recordCnt++;
}



any insight or direction would be appreciated. Thank You,

wysota
28th August 2017, 15:45
connect(this, &DataStreamToBuffer::DataToVector_Signal, workerThread, &DataStreamToVector::parseStreamToVector_Slot, Qt::DirectConnection);
Why is this a direct connection? This is definitely incorrect as it makes your parseStreamToVector function execute in the main thread instead of the worker thread which is probably not what you want.

swankster
28th August 2017, 16:11
I originally had Qt::QueuedConnection and tried direct but forgot to put it back. either way returned the same result.

wysota
29th August 2017, 06:30
Please run your application under a debugger. When the freeze occurs, break the application, dump the backtrace and show it to us.

swankster
29th August 2017, 12:44
I ran in debug before an it did not fail. Ran it again last night and it did. It appears thread and vector are working fine. It thinks my MySQL table has been truncated.
Thanks for having me check the obvious.

swankster
30th August 2017, 12:58
this post is [SOLVED] the issue has nothing to do with QThread or QVector. It is MySQL connection getting hung-up.