Running glutMainLoop from separate thread

Hi

I’ve made a cross platform application running OpenGL through glut. When porting it to mac the window stopped to respond to mouse clicks and repaint events.

This piece of code shows the error (tested on MacOS 10.3). If undefining THREADED it will work properly.

// GLUT problem
// Build with g++ -framework GLUT -framework OpenGL -framework Foundation -o pthglut *.cpp

#include <iostream>
#include <pthread.h>
#include <GLUT/glut.h>

void display(){
	glClear(GL_COLOR_BUFFER_BIT);
	glutSwapBuffers();
}

void menuFunc(int i){
	exit(0);
}


static void* glutThreadFunc(void* v){
	int argc = 1;
	char *argv[] = {"x"};
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(320, 240);
	glutCreateWindow("pthglut!!111!");
	glutDisplayFunc(display);
	int glmenu = glutCreateMenu(menuFunc);
	glutAddMenuEntry("Quit", 1);
	glutAttachMenu(GLUT_RIGHT_BUTTON);
	glutMainLoop();
}

#define THREADED

int main(){
	pthread_t glutThreadId;
#ifdef THREADED
	pthread_create(&glutThreadId, NULL, glutThreadFunc, 0);
	while(true){
		sleep(100);
	}
#else
	glutThreadFunc(0);
#endif
}

Has anyone any ideas on what’s going on?
Thanks
/David

as answered on iDevGames, GLUT can only be used from the main thread.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.