PDA

View Full Version : Threads: segmentation fail with GL-functions calls



Mufan
07-10-2011, 08:21 AM
Hi all!

I'm coding via Debian 6.0.1, C (without C++).
I create a new thread in my program via "pthread_create()" function. In this new thread I want to doing smth via GL-functions. For example, I tried to call glGetFloat() function. But I had segmentation fail there. So, any gl<...> functions make me the same error.

glu<...> or glut<...> functions are working correctly in the threads.

What's can be wrong there?

Thanks!
--
Best regards, Serge.

Aleksandar
07-10-2011, 08:29 AM
Something is not clear... How can glu functions work, while gl don't?
glu is just a wrapper around gl. You have to make GL context current for the calling thread. Be aware that GL context can be current in one thread only (one at the time).

Mufan
07-11-2011, 10:58 AM
I have this main function in my program:


int main (int argc, char *argv[])
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(
GLUT_DOUBLE |
GLUT_DEPTH |
GLUT_RGBA |
GLUT_MULTISAMPLE);

glutGameModeString("800x600:32@60");
glutEnterGameMode();
glutSetCursor(GLUT_CURSOR_NONE);

glutKeyboardFunc(<...>);
glutSpecialFunc(<...>);
glutDisplayFunc(<...>);
glutReshapeFunc(<...>);
glutMouseFunc(<...>);
glutMotionFunc(<...>);
glutIdleFunc(<...>);
glutCloseFunc(<...>);

pthread_attr_init(&amp;Threads_Init_Attribute);
if(pthread_getschedparam(pthread_self(), &amp;Threads_Init_Policy, &amp;Threads_Init_Schedule_Params) != 0)
{
return 0;
}
if(pthread_create(&amp;Thread_Init, &amp;Threads_Init_Attribute, (void *(*)(void*))func_thread, 0) != 0)
{
return 0;
}

glutMainLoop();
return 0;
}


And this for the thread:



void func_thread()
{
}


So, I can transfer this code to my thread function from my main function:



glutKeyboardFunc(<...>);
glutSpecialFunc(<...>);
glutDisplayFunc(<...>);
glutReshapeFunc(<...>);
glutMouseFunc(<...>);
glutMotionFunc(<...>);
glutIdleFunc(<...>);
glutCloseFunc(<...>);


Also, I can write here something like this (for the glu functions):



int *viewport = (int*)malloc(sizeof(int)*4);
double *mvmatrix = (double*)malloc(sizeof(double)*16);
double *projmatrix = (double*)malloc(sizeof(double)*16);

double l_x;
double l_y;
double l_z;

gluProject(0.0, 0.0, 0.0,
mvmatrix, projmatrix, viewport,
&amp;l_x, &amp;l_y, &amp;l_z
);
free(viewport);
free(mvmatrix);
free(projmatrix);


And it works fine. But if I call some gl function in func_thread(), there will be the segmentation fail. Ex., glShadeModel(GL_SMOOTH);

V-man
07-12-2011, 08:35 AM
In the first thread, you need to make the GL context non current with a call to glXMakeCurrent. Put NULL for the GL render context.

Now, on the secondary thread, you can make it current.

Mufan
07-13-2011, 09:24 AM
V-man, I have just read info about glXMakeCurrent. Do I understand it right: I need to make Display, XtAppContext, XVisualInfo variables, set some int variable as ScreenID... Then set Display with XtDisplay... Then use glXChooseVisual...And only this moment set glxcontext...

Is that absolutly another technology to setup an OpenGL canvas?
I just create glutWindow (code in the post #3), and it almost makes me happy.

Or if I want to work with threads, I need to make that big (see the top of the post) setup anyway?