Threads: segmentation fail with GL-functions calls

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.

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).

I have this main function in my program:


int main (int argc, char *argv[])
{
  glutInit(&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(&Threads_Init_Attribute);
  if(pthread_getschedparam(pthread_self(), &Threads_Init_Policy, &Threads_Init_Schedule_Params) != 0)
  {
    return 0;
  }
  if(pthread_create(&Thread_Init, &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,
	      &l_x, &l_y, &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);

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.

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?