Hello All,

I made a data acquisition software with the worker/DAQ code in one thread and the visualization/GUI in the other. My approach is using this method. I'm trying to communicate between these threads using signal/slot connections by emitting a void dataUpdate(unsigned char *buf); from the worker thread and visualizing the data in the GUI thread.

The problem is that I'm not using any mutex whatsoever and I'm quite certain that the same memory gets accessed by both the threads at the same time, thus displaying bogus poo in my graph every few frames.

I want to implement a QMutex or QSemaphore of some sort, although I'm a bit apprehensive about threads waiting for one another. Under no circumstance do I want the worker (acquisition) thread to be waiting to have write access to the variable, because if the DAQ thread starts waiting for GUI, I start losing data.

My approach is different to the mandelbrot example due to the fact that the data processing gets done in the GUI, while the DAQ thread focuses solely on acquisition. Example:

Qt Code:
  1. class myDAQ : public QObject{ //this is the one that should have no dead time
  2. public:
  3. myDAQ(){
  4. /*constructor stuff here*/
  5. buff=new unsigned char[2048];
  6. }
  7.  
  8. public slots:
  9. void acquireData(){
  10. //get the data;
  11. emit sendToBeGraphed(buff);
  12. }
  13.  
  14. signals:
  15. void sendToBeGraphed(unsigned char *buf);// usually a few thousand bytes worth of data
  16.  
  17. private:
  18. unsigned char *buff;
  19. };
  20.  
  21. class myGUI : public QMainWindow{
  22. public:
  23. myGUI(){
  24. thread=new QThread;
  25. acquisitionClass=new myDAQ;
  26.  
  27. connect(acquisitionClass,SIGNAL(sendToBeGraphed(unsigned char*)),this,SLOT(visProc(unsigned char*)));
  28.  
  29. acquisitionClass->moveToThread(thread);
  30. thread->start();
  31. }
  32.  
  33. public slots:
  34. void visProc(unsigned char *raw){
  35. decodeData();
  36. plotData();
  37. }
  38.  
  39. private:
  40. QThread *thread;
  41. myDAQ *acquisitionClass;
  42.  
  43. }
To copy to clipboard, switch view to plain text mode 

How do I ensure that when visProc() gets called, the buff variable is not being modified? I was thinking to mutex.lock() inside the myDAQ::acquireData(), but would I have to mutex.lock() in the GUI class as well?

Thanks


Regards,
Mr_Cloud