Threading in Linux and MacOS



  • I'm having some strange behavior of my application and I really need some help. My app computes an algorithm and a opengl widget plots the result of that algorithm. The result is a vector of pointers to float (std::vector<float*> result), so I'm plotting several functions at the same time in XY coordinates. In the MainWindow.cpp I am using:

    @
    uiPlot->plotStart(); //Shows the opengl widget and starts a QTimer.
    runThread = QtConcurrent::run(this,&MainWindow::mainLoop);
    runWatcher.setFuture(runThread);
    @

    where runThread is a QFuture<void> object and runWatcher is a QFutureWatcher<void> object. The class uiPlot is a Dialog that contains a QGLWidget object where I plot all my functions and it contains also a QTimer that updates the plotting every "fps" frames per second. The mainLoop function has an endless loop that computes the vectors and also a delay using QWaitCondition:

    @
    while(1){
    computeAll( input ); //Updates the content of all vectors in "result".

    mutex.lock();
    waitCondition.wait(&mutex, 1000/fps);
    mutex.unlock();
    

    }
    uiPlot->plotStop(); // Stops the timer and closes the widget.
    @

    My problem: I don't understand why the application works fine if I want to plot a single function (result.size() = 1) but crashes when I want to plot several functions at the same time(result.size() > 1)... this is only in Linux because it works for any sizes in MacOS. One more mysterious thing, if I debug the application everything works fine in Linux, it is only when I'm running it (without debugging) when it crashes.

    Any help will be very much appreciated,

    B



  • Sounds to me like you have timing issues somewhere.



  • I guess you problem is with threads, std::vector<T> is not implicitly shared, user QVector<T> instead it is implicitly shared, it can work across threads.



  • [quote author="Santosh Reddy" date="1308202268"]I guess you problem is with threads, std::vector<T> is not implicitly shared, user QVector<T> instead it is implicitly shared, it can work across threads.[/quote]That does depend on how you use it. If you have multiple threads accessing the same instance of the vector, implicit sharing won't help you. But when taking the approach of creating and filling a container and then copying the contents to a shared location, it certainly will.



  • QVector is not threadsafe, and neither is std::vector. If you have a shared resource, accesses to it must be serialized.

    Another question, why are you using a pointer to a float rather than just a float in your vector? You are not using any less memory.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.