Today i’ve added a multi threading option to my API :
instead of calling “init()” at the beginning, the user can call “initNewThread()”, which will launch the API and start refreshing the window in a secondary thread.
After that the user can load the trajectory, borehole, bha etc… from the main thread, and still use the window at the same time.
Also, the user can programmatically control the window( Like setting the time for example) without having to handle the refresh loop (like it was before).
Finally, I’ve added a progress bar and an “abort” button in order to follow the loading process and be able to cancel if it’s too long.
Problems encountered
My classes were not thread safe (the mutex were not used correctly, for example I was locking a resource,returning a reference, unlocking the resource, and then using the reference.) So now this is all fixed. (I’ve added explicit functions to lock /unlock the data)
OpenGL calls can only be made from the thread in which the OpenGL context has been created, 2 solutions to fix this problem :
- create a second context in the second thread and share the resources: this solution was not easy to implement with my design (the opengl context is managed by wx, so I should have created a new window, and also it means I should check before each openGL call that the right context is active).
- send messages to the thread which can use opengl so that it makes the call for the other thread : this is the solution I’ve implemented; at the beginning of each “update” loop the secondary thread check if there are some opengl requests coming from the other thread. In the meantime the other thread is blocked and wait for the other thread answer.
So far it’s working fine like this.
There are two problems left :
- when I close the application the two threads don’t seem to synchronize correctly.
- the shaders don’t seem to work for the moment.
I’ll check that tomorrow.