QThread or QTimer advice
I am trying to build a data acquisition application and I am struggling with the best way to capture the incoming data.
The data arrives at a rate of 5 packets of 10K integers per second. My original idea was to just use a thread to check if data was available and if so get it to process and if not sleep for 20 ms. This locks up the qt event loop and is probably not such a good idea.
Another alternative is to use a timer set to 20 ms that will check for data available. The problem I have thinking about this is that if I get data then I want to ignore several timer events while I process the current block of data. When I have processed the data I want to start getting timer events again. QTimer does not have a pause.
I am thinking of a two stage approach where the first stage knows if a block is being processed and throws away the timer events and if a block is not being processed ask if data is available.
Any suggestions would be appreciated.
First a question, how does a thread block your event loop? One of the reasons to use threads in the first place is not to block the event loop.
In any case, what you could is have one thread put the data in a queue, and have another thread (or threads) process the data out of that queue.
The first thread can use a simple timer, and if acquiring the data is fast you won't miss any events. The second thread (or threads) just take the data from the queue and processes it.
Be sure to make your queue thread-safe and so on.
Ps. QTimer has a stop and restart function.
My thread function was:
and I wanted to be able to turn on and off isStarted from two slots.
isStarted = true;
isStarted = false;
Because of the outer loop in runIt(), startIt() would never execute until I found the function and put it before the sleep.
How are you using that class in your code ?
The class has 3 slots that invoke runIt, startInput, and stopInput; The ui has two buttons that are connected to the startInput and stopInput slots. The thread starts the runIt method.
The code in the outer class that hooks it all together is:
thread = new QThread;
service = new Service();
connect(thread, SIGNAL(started()), service, SLOT(runIt()));
connect(session, SIGNAL(startInput()), service, SLOT(startInput()));
connect(session, SIGNAL(stopInput()), service, SLOT(stopInput()));
connect(service, SIGNAL(writeData(int*)), session, SLOT(writeData(int*)));
Just thought of something, where are you data coming from ?
I haven't connected the back end yet, but it is coming from a device that has a dll to provide access to the data. the flow of control I use is
int status = abcReadStatus();
foo = abcReadFrame(false);
writeData(foo); // a connection out of the thread for processing