May 21, 2011

TheDestroyer TheDestroyer
Lab Rat
150 posts

QThread real control

 

Hello guys :),

I would like to parallelize my simulation… but I really have gone crazy because every method I try for using QThread fails. Either by not having access to the object inherited from QThread because it doesn’t support Widgets, or because of a segmentation fault that I don’t understand and doesn’t make sense at all!

Structure of my simulation:

The structure of a simulation program I’m creating is as follows:

MainWindow contains an OpenGLWidget that contains an object that has the simulation tools, parameters, equations and functions. MainWindow contains also options Dialogs that control the parameters in OpenGLWidget and the simulation object contained in it.

The simulation starts with initial values. OpenGLWidget draws the initial values. The simulation object which is inside the OpenGLWidget has a function called advanceTimeStep(). This function advances the simulation to the next step by solving some equations. After drawing the initial values in OpenGLWidget, OpenGLWidget calles the function advanceTimeStep() which advances the simulation and gets new values to be drawn in OpenGLWidget.

To ensure that the speed of the rendering is independent of the computer speed, the time is measured between every two contiguous renderings of OpenGL. If more than 1 ms has passed, then more advanceTimeStep()s are called to substitute the time lost in rendering and retain a constant speed for the simulation.

Parallelising:

I would like to let openGL process in a thread, and the simulation in another thread. So I’m not sure to do this. The first thing I did is putting the simulation object inside a thread. Which is a class inherited from QThread. So in the class body, I defined the simulation object as a pointer. And in the function run(), I defined the memory allocation operator “new” for my simulation object. This didn’t work because I didn’t know how to use advancedTimeStep() in it!!!

The call was done by calling thead::start() function.

I’ve tried also many other ways like putting advanceTimeStep() in run(). And failed also in using the thread as a widget, which I need actually where I could have a thread for openGL.

Questions:

-What’s the best way to parallelise such a system?
-Is there a way to have OpenGL in a thread and the simulation object in another thread and have them both effectively linked to exchange data?
-Does calling the function thread::start() keep looping run()??? how would I use thread::start() in my case??

Thanks you :)

3 replies

May 21, 2011

TheDestroyer TheDestroyer
Lab Rat
150 posts

Come on guys… anything that helps :(

May 21, 2011

koahnig koahnig
Area 51 Engineer
2891 posts

Hi TheDestroyer,
I have only limited experience with threading, but it wasn’t very complicated.
Have you started reading with that part? [doc.qt.nokia.com]

May 21, 2011

koahnig koahnig
Area 51 Engineer
2891 posts

I was checking some of my code. You better derive a class from QThread. The derived class needs to have a run method for actually doing whatever it has to do.
My run looks like:

  1. void ThreadSend :: run ()
  2. {
  3.     forever
  4.     {
  5.          ...
  6.     }
  7. }

The forever statement is not a joke. It is part of Qt.

Maybe you should have a look to this example [doc.qt.nokia.com]

 
  ‹‹ run an .exe from other’s application function      Dependent actions ››

You must log in to post a reply. Not a member yet? Register here!