Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I can't be completely sure what the problem is since I have no clue what the <code>ShowSensorData()</code> function (method?) is doing, but there are some multithreading issues with the code that you have included.</p> <ol> <li><code>mUpdate</code> should be protected by a mutex if it is accessed by more than one thread.</li> <li>The <code>run()</code> method looks like it will lock the mutex and never release it if <code>mStop</code> is true.</li> </ol> <p>You should consider using <a href="http://en.wikipedia.org/wiki/RAII" rel="nofollow noreferrer">RAII</a> practices to grab and release the mutex. I don't know if you are using Qt mutexes or not but you should look into using <a href="http://doc.trolltech.com/4.5/qmutexlocker.html" rel="nofollow noreferrer">QMutexLocker</a> to lock and unlock your mutexes.</p> <p>I would consider changing your <code>SensorProtocol</code> class to use the condition variable and a flag or some sort of event (not sure what Qt has to offer here) to handle the update inside of a method associated with the object instance. Something like:</p> <pre><code>/*static*/ void SensorProtocol::updateSensorProtocol() { SensorProtocol *inst = SensorProtocol::getInstance(); inst-&gt;update(); } </code></pre> <p>Then make sure that the <code>update()</code> method grabs the mutex before reading or writing any of the members that are shared between the reader and display.</p> <p>A more complete approach would be to separate your UI display, the sensors, and their linkage using a <a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller" rel="nofollow noreferrer">Model-View-Controller</a> architecture. Refactoring the solution into an MVC architecture would probably simplify things quite a bit. Not to mention that it makes applications like this a lot less error-prone. Take a look at the <a href="http://doc.trolltech.com/4.5/qabstractitemview.html" rel="nofollow noreferrer">QAbstractItemView</a> and <a href="http://doc.trolltech.com/4.5/qabstractitemdelegate.html" rel="nofollow noreferrer">QAbstractItemDelegate</a> classes for an idea on how this can be implemented. From what I remember, there is a tutorial about implementing MVC using Qt somewhere... it's been quite a few years since I have played with Qt though.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload