New Posts  All Forums:Forum Nav:

OpenGL with Pthreads

post #1 of 9
Thread Starter 
I'm trying to make a multithreaded app that uses OpenGL. For testing purposes I took the Red Book's hello world program and threw it into a second thread. When I run it, I get a plain white box with no contents or titlebar. The program takes 100% CPU time and stops responding after a few seconds.

If I nix the thread and call the gl_main function directly from the primary thread, the program runs as normal. Anybody have an idea what's going on? glutMainLoop does get called, but it does nothing except load the CPU.

Here is the code

Code:
#include <GLUT/glut.h>
#include <pthread.h>
#include <stdio.h>

int *g_argc;
char **g_argv;

void display(void){
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glVertex3f(0.25, 0.75, 0.0);
glEnd();

glFlush();
}

void init(void){
glClearColor(0.0, 0.0, 0.0, 0.0);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

void *g_main(void *arg){
int i;

printf("Argc: %dn", *g_argc);
for(i=0; i<*g_argc; i++)
printf("Argv[%d]: %sn", i, g_argv[i]);

glutInit(g_argc, g_argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(250, 250);
glutInitWindowPosition(100, 100);
glutCreateWindow("Hello, world");
init();
glutDisplayFunc(display);
glutMainLoop();
return NULL;
}

int main(int argc, char **argv){

g_argc = &argc; g_argv = argv;
pthread_t thread;
pthread_create(&thread, NULL, &g_main, NULL);
//g_main(NULL);

char c;
printf("Will exit if you enter a charn");scanf("%c", &c);
}
This was written under OS X. To compile under linux the include should be changed to <GL/glut.h>. I have no idea if this will compile under windows.
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #2 of 9
This isn't based off much of anything but you might try creating the window in the main and then updating it in a seperate thread.
Monster v2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 GA-P35-DS3L HD 4850 6GB Crucial Ballistix 
Hard DriveOSMonitorKeyboard
WD 320GB Windows 7 Ultimate (64bit) 2x Acer 22" WS Logitech g15 
PowerCaseMouse
OCZ 700W Gigabyte 3D aurora Logitech Mx510 
  hide details  
Reply
Monster v2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 GA-P35-DS3L HD 4850 6GB Crucial Ballistix 
Hard DriveOSMonitorKeyboard
WD 320GB Windows 7 Ultimate (64bit) 2x Acer 22" WS Logitech g15 
PowerCaseMouse
OCZ 700W Gigabyte 3D aurora Logitech Mx510 
  hide details  
Reply
post #3 of 9
I just sent that code snippet to one of my friends (hope that is OK). He is an OpenGL genius! If he can't figure it out...
Gaming rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
C2D E8600 ASUS Striker Extreme eVga 8800 gts 640 OCZ Platinum 4gb 
Hard DriveOptical DriveOSMonitor
WD2500JS NEC DVD+-RW Windows XP SP3 2x LG L226WTX 22" widescreen 
KeyboardPowerCaseMouse
Ideazon Merc Stealth Ultra Atx 600 Cooler Master CM690 Logitech Mx 518 
  hide details  
Reply
Gaming rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
C2D E8600 ASUS Striker Extreme eVga 8800 gts 640 OCZ Platinum 4gb 
Hard DriveOptical DriveOSMonitor
WD2500JS NEC DVD+-RW Windows XP SP3 2x LG L226WTX 22" widescreen 
KeyboardPowerCaseMouse
Ideazon Merc Stealth Ultra Atx 600 Cooler Master CM690 Logitech Mx 518 
  hide details  
Reply
post #4 of 9
Thread Starter 
There's still the CPU load and non responsiveness, but I do get a valid window now. Thanks a lot
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #5 of 9
Hmm, well that gets me good and curious... If I remember I'll mess with it when I get home from work.
Monster v2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 GA-P35-DS3L HD 4850 6GB Crucial Ballistix 
Hard DriveOSMonitorKeyboard
WD 320GB Windows 7 Ultimate (64bit) 2x Acer 22" WS Logitech g15 
PowerCaseMouse
OCZ 700W Gigabyte 3D aurora Logitech Mx510 
  hide details  
Reply
Monster v2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 GA-P35-DS3L HD 4850 6GB Crucial Ballistix 
Hard DriveOSMonitorKeyboard
WD 320GB Windows 7 Ultimate (64bit) 2x Acer 22" WS Logitech g15 
PowerCaseMouse
OCZ 700W Gigabyte 3D aurora Logitech Mx510 
  hide details  
Reply
post #6 of 9
Thread Starter 
Wiki has the answer

Quote:
The library requires programmers to call glutMainLoop(), a function which never returns. This makes it hard for programmers to integrate GLUT into a program or library which wishes to have control of its own event loop. A common patch to fix this is to introduce a new function, often called glutCheckLoop(), which runs only a single iteration of the GLUT event loop. Another common workaround is to run GLUT's event loop in a separate thread, although this may vary by operating system, and also may introduce synchronization issues or other problems: for example, the Mac OS X GLUT implementation requires that glutMainLoop() be run in the main thread.


I'm probably going to nix GLUT and go with GLX. Now I just have to find a good reference
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #7 of 9
Ahh, that explains it. I did my openGL stuff in Qt for my schoolwork, haven't messed with it since I graduated but that's good to know
Monster v2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 GA-P35-DS3L HD 4850 6GB Crucial Ballistix 
Hard DriveOSMonitorKeyboard
WD 320GB Windows 7 Ultimate (64bit) 2x Acer 22" WS Logitech g15 
PowerCaseMouse
OCZ 700W Gigabyte 3D aurora Logitech Mx510 
  hide details  
Reply
Monster v2
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 G0 GA-P35-DS3L HD 4850 6GB Crucial Ballistix 
Hard DriveOSMonitorKeyboard
WD 320GB Windows 7 Ultimate (64bit) 2x Acer 22" WS Logitech g15 
PowerCaseMouse
OCZ 700W Gigabyte 3D aurora Logitech Mx510 
  hide details  
Reply
post #8 of 9
Hello guys, I am trying to combine Pthreads with OpenGL(glut) applications. Could anybody give me a simple way of using pthreads in an OpenGL application?

Thank you.
post #9 of 9
Thread Starter 
What do you want to know? What are you trying to do?
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming