OpenGL + POSIX thread = Segmentation fault. Why? %(

When I try to draw some objects in pthread function programm breaks (segmentation fault)…

Same example:

void *compile_events(void *aSuper) {
class MainGL super;
super = (class MainGL
) aSuper;
while(1) {
super->drawSomething();
}
}

MainGL is a simple class which has a display, reshape & other same openGL functions (friend functions of class).

Please, help me if you understand my problem.

Hi !

There can be lots of stuff that can go wrong with threads, but first of all, do you have the rendering context active ? is it in the same thread or another thread ?

You may also find it very useful to find out why and where it crashes with a debugger.

Mikael

I use 2 threads: glutMainLoop (main) and one posix thread. I know where it crashes - if I don’t draw anything in my draw-function, it works normal, so, the problem is somewhere in openGL or… maybe this 2 threads can’t work right together!?

I tried:
If I call my draw-function form main everything is good, but if I call the same draw-function from posix thread - it crashes with segmentation fault %(

TNX

If you use GLUT, then GL is created in the main thread and you can only make GL calls from there.

You have to dump GLUT and use something better.

Well, each thread has its own rendering context. If you created the window in main, then main owns the context, and that’s the only place where you can draw.

Don’t even think about drawing to the same context from different threads. By bypassing GLUT you could make it work, but it is not worth it since it will kill performance. Better do geometry calculations etc in one thread and then draw in the main thread (using proper mutex + condition variable synchronization).