LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-08-2007, 07:37 AM   #1
Winter Knight
Member
 
Registered: Nov 2005
Distribution: Debian Stable/Testing
Posts: 54

Rep: Reputation: 15
Question Using OpenGL with threads?


OpenGL is not thread safe. I understand that (or maybe not). I have heard that all OpenGL calls have to be made in the original thread. I don't understand why that would be. It seems to me that as long as you don't call multiple OpenGL calls simultaneously, that it would work. However, by observation, it seems that it is true. I still don't understand why, though.

Here is a simple application that seems, to me, like it should work. But it segfaults.

Code:
// Compile this program with "gcc -o tmp tmp.c `sdl-config --libs` -lGL"

#include "SDL/SDL.h"
#include "SDL/SDL_thread.h"

#include "GL/gl.h"
#include <unistd.h>

int my_thread(void * data) {
	glGetString(GL_VERSION );
}

int main(int argc, char ** argv) {
	SDL_Init(SDL_INIT_VIDEO);
	SDL_SetVideoMode(320, 240, 32, SDL_OPENGL);

	SDL_Thread * test = SDL_CreateThread(my_thread, NULL);

	SDL_WaitThread(test, NULL);

	SDL_Quit();

	return 0;
}
Can someone explain why this program segfaults? Is there a simple way to fix it? Is there any way at all that opengl calls can be made in non-primary threads?
 
Old 10-08-2007, 11:11 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
I have heard that all OpenGL calls have to be made in the original thread. I don't understand why that would be/
This is because OpenGL is a state machine and the state will be shared with multiple threads; thus meaning there could be a data race at any time. So if this is what you need to do, then the design needs changing.
 
Old 10-08-2007, 11:46 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
I have heard the same thing about OpenGL. The usual solution I think is to have one thread which does OpenGL calls, and farm out non-gl functions to other threads, e.g. game physics or other calculations, but only draw them in the GL thread.
 
Old 10-10-2007, 03:22 AM   #4
Winter Knight
Member
 
Registered: Nov 2005
Distribution: Debian Stable/Testing
Posts: 54

Original Poster
Rep: Reputation: 15
Thanks, guys. Is putting all OpenGL calls in the original thread the only way to solve this? Is there any other way at all?

Quote:
OpenGL is a state machine and the state will be shared with multiple threads; thus meaning there could be a data race at any time. So if this is what you need to do, then the design needs changing.
If they all share the same state, how could there be a data race? Wouldn't they all have the exact same state, and therefore be perfectly in sync? Or did I misunderstand you?
 
Old 10-10-2007, 06:08 AM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Think of the OpenGL state as a global variable and there are multiple threads which can change or access it. Is it safe to do this without some form of mutual exclusion?

The problem is you can not add a mutual exclusion to the OpenGL state.

Normally you have a renderer (which may be in its own thread), you send data batches in the form of vertex data(FFP) or something similar and the renderer makes all the API calls.

Last edited by dmail; 10-10-2007 at 06:15 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
"Find all threads started by user" not showing all threads Nylex LQ Suggestions & Feedback 3 12-28-2005 08:28 PM
Java threads listed using kill -3 does not contain all threads found using ps -auxww coneheed Programming 2 11-14-2005 08:57 AM
Overwrite Mesa OpenGL with ATI OpenGL Carl-Fredrik Slackware 12 10-01-2004 03:33 PM
Java Threads vs Native Threads rjmendez Programming 0 08-16-2004 05:58 AM
Changing from MESA OpenGL to ATI OpenGL tillyoubreakit Linux - Hardware 19 10-07-2003 07:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration