pthread and opengl

L

Leon

Hey, i am trying to develop a game, and I am using OpenGL and pthread.
(on Ubuntu, so i also use the X library.) Now i have a problem. I have
initialized OpenGL in the main thread, and i want to repaint 30 times
every second (the usual framerate). Now i programmed a timer, which was
able to wait for 30/1000 seconds. That happens in another thread, and
after the delay (nanosleep, from the time.h header), it calls the paint()
function. And it returned a error message. I found out that it was
because OpenGL can't be painted from a different thread than the
initialising thread. Can anyone help me what to do to fix this? I can't
let the initialising thread delay and repaint, because it has to wait for
X inputs.

(main_thread)
|
\ /

(initialize OpenGL)
|
\ /

(start painting timer)
| \
\ / \
(wait for X events, XNextEvent()) |
\ /
(wait 30/1000 second, and then
call the repaint function)


Can anyone help me please?

grz, Leon
 
D

dave_mikesell

Hey, i am trying to develop a game, and I am using OpenGL and pthread.

You're better off asking in comp.graphics.api.opengl - this group only
deals with standard C++.

Keep in mind, though, that OpenGL is a big state machine and it's not
thread safe, so you have to manage thread access to the OpenGL
context.
 
R

Rolf Magnus

Leon said:
Hey, i am trying to develop a game, and I am using OpenGL and pthread.

Both OpenGL and pthreads are off-topic here. A better newsgroup for your
question is comp.graphics.api.opengl.
I found out that it was because OpenGL can't be painted from a different
thread than the initialising thread. Can anyone help me what to do to fix
this?

Most OpenGL implementations aren't thread-safe. You must do all the OpenGL
calls in one thread.
 
L

Leon

You're better off asking in comp.graphics.api.opengl - this group only
deals with standard C++.

Keep in mind, though, that OpenGL is a big state machine and it's not
thread safe, so you have to manage thread access to the OpenGL context.

Okay, thank you
 
J

James Kanze

Both OpenGL and pthreads are off-topic here. A better newsgroup for your
question is comp.graphics.api.opengl.
Most OpenGL implementations aren't thread-safe. You must do
all the OpenGL calls in one thread.

Isn't this more or less true of all Windowing frameworks? I
would expect the usual architecture to have a dedicated thread
for handling the graphics on the screen, with at the most the
model component being accessible from more than one thread.

Which isn't quite the same thing as his problem, if I understand
it correctly. He initializes the graphics from the "original"
thread (in fact, before starting threading), and then wants to
serve them from another, dedicated thread. Normally, I would
expect this to work, as long as the initializing thread doesn't
do anything with the objects after the server thread has been
started. But I can imagine a debugging implementation which
notes which thread it was created in, and asserts that all of
the following requests are in that thread. The solution, of
course, is to move the initialization to the graphics thread. I
tend to do this anyway if the initialization is non-trivial; to
me, it seems somehow cleaner.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top