Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 06-28-08   #1 (permalink)
Kernel Sanders
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,897
Blog Entries: 1

Rep: 549 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 327
FAQs Submitted: 6
Trader Rating: 5
Default OpenGL with Pthreads

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.
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 07-01-08   #2 (permalink)
Programmer
 
intel ati

Join Date: Aug 2006
Location: Moscow, ID
Posts: 154

Rep: 10 Safetydan Unknown
Unique Rep: 9
Trader Rating: 1
Default

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.
__________________
"He attacked everything in life with a mix of extraordinary genius and naive incompetence, and it was often difficult to tell which was which."
Douglas Adams

System: Monster v2
CPU
Q6600 G0
Motherboard
GA-P35-DS3L
Memory
2GB Crucial Ballistix
Graphics Card
Saphire Radeon x1900XTX
Hard Drive
WD 320GB
Sound Card
x-fi extreme music (modded)
Power Supply
OCZ 700W
Case
Gigabyte 3D aurora
CPU cooling
TR Ultra-120 Extreme
GPU cooling
vf900
OS
Windows XP
Monitor
2x Acer 22" WS
Safetydan is offline   Reply With Quote
Old 07-01-08   #3 (permalink)
*cough* Stock *cough*
 
intel nvidia

Join Date: Dec 2007
Location: Santa Cruz Mountains
Posts: 404

Rep: 45 airbozo is acknowledged by some
Unique Rep: 41
Trader Rating: 5
Default

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...
__________________
"The computer can't tell you the emotional story. It can give you the exact mathematical design, but what's missing is the eyebrows. "...Frank Zappa...
Edit/Delete Message

System: Gaming rig
CPU
Xeon 5160 x2
Motherboard
Tyan 2692
Memory
ATP FBDIMM 1gb x4
Graphics Card
eVga 8800 gts 640
Hard Drive
WD2500JS
Sound Card
On Board
Power Supply
Ultra Atx 600
Case
Cooler Master CM690
CPU cooling
Stock
GPU cooling
Stock
OS
Windows XP SP3
Monitor
2x LG L226WTX 22" widescreen
airbozo is offline   Reply With Quote
Old 07-01-08   #4 (permalink)
Kernel Sanders
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,897
Blog Entries: 1

Rep: 549 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 327
FAQs Submitted: 6
Trader Rating: 5
Default

There's still the CPU load and non responsiveness, but I do get a valid window now. Thanks a lot
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 07-01-08   #5 (permalink)
Programmer
 
intel ati

Join Date: Aug 2006
Location: Moscow, ID
Posts: 154

Rep: 10 Safetydan Unknown
Unique Rep: 9
Trader Rating: 1
Default

Hmm, well that gets me good and curious... If I remember I'll mess with it when I get home from work.
__________________
"He attacked everything in life with a mix of extraordinary genius and naive incompetence, and it was often difficult to tell which was which."
Douglas Adams

System: Monster v2
CPU
Q6600 G0
Motherboard
GA-P35-DS3L
Memory
2GB Crucial Ballistix
Graphics Card
Saphire Radeon x1900XTX
Hard Drive
WD 320GB
Sound Card
x-fi extreme music (modded)
Power Supply
OCZ 700W
Case
Gigabyte 3D aurora
CPU cooling
TR Ultra-120 Extreme
GPU cooling
vf900
OS
Windows XP
Monitor
2x Acer 22" WS
Safetydan is offline   Reply With Quote
Old 07-01-08   #6 (permalink)
Kernel Sanders
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,897
Blog Entries: 1

Rep: 549 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 327
FAQs Submitted: 6
Trader Rating: 5
Default

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
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 07-02-08   #7 (permalink)
Programmer
 
intel ati

Join Date: Aug 2006
Location: Moscow, ID
Posts: 154

Rep: 10 Safetydan Unknown
Unique Rep: 9
Trader Rating: 1
Default

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
__________________
"He attacked everything in life with a mix of extraordinary genius and naive incompetence, and it was often difficult to tell which was which."
Douglas Adams

System: Monster v2
CPU
Q6600 G0
Motherboard
GA-P35-DS3L
Memory
2GB Crucial Ballistix
Graphics Card
Saphire Radeon x1900XTX
Hard Drive
WD 320GB
Sound Card
x-fi extreme music (modded)
Power Supply
OCZ 700W
Case
Gigabyte 3D aurora
CPU cooling
TR Ultra-120 Extreme
GPU cooling
vf900
OS
Windows XP
Monitor
2x Acer 22" WS
Safetydan is offline   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 11:55 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.15016 seconds with 9 queries