Multithreaded data processing pipeline in Qt Multithreaded data processing pipeline in Qt multithreading multithreading

Multithreaded data processing pipeline in Qt


Just build your own one-element "queue" class. It should have:

A piece of data (or pointer to data)A Boolean "dataReady"A mutexA condition variable

The "enqueue" function is just:

lock mutexReplace data with new datadataReady = truesignal condition variable

The "dequeue" function is just:

lock mutexwhile (!dataReady) cond_wait(condition, mutex)tmpData = datadata = NULL (or zero)dataReady = falseunlock mutextreturn tmpData

The type of the data can be a template parameter.


What you are dealing with is a Producer Consumer Pattern. You can find a general overview of that here. http://en.wikipedia.org/wiki/Producer-consumer_problem

You want to use a QMutex to limit access to the data to one thread at a time. Use the QMutexLocker to lock it.

For a VERY simplified example:

QList<quint32> data;QMutex mutex;//  Consumer Thread calls thisint GetData(){   quint32 result(-1); // if =1 is a valid value, you may have to return a bool and                        // get the value through a reference to an int                        // in the parameter list.   QMutexLocker lock(&mutex);   if (data.size())   {      result = data.front();  // or back      data.clear();   }   return result;}// Producer Thread calls thisvoid SetData(quint32 value){    QMutexLocker lock(&mutex);    data.push_back(value);}