Trying to do something with a thread.

Ok so this is my first time using a thread and I'm a little confused. I have a function that I want to run separate and it doesn't need to know about thing else but itself.

I'm trying to use it like this compiles but no luck.

Function telling the thread to draw?
1
2
3
4
5
6
DWORD WINAPI ThreadProc(LPVOID lpParam)
{
	TileMap* t = (TileMap*)lpParam;
	t->Draw();
	return 0;
}


Creating the thread.
1
2
3
4
5
TileMap* Zone1 = new TileMap();
Zone1->SetMap("Data\\TileMap.map");

DWORD ThreadID;
CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)ThreadProc, Zone1, 0, &ThreadID);


This might not even be possible but I just want to start this thread and draw it in the background in my main loop.

Thanks if anyone can figure out what I'm doing wrong or if this is even possible.
Looks correct to me. Is TileMap thread-safe?
Define thread safe? It just has a loop that draws(binds) a texture with openGL. Nothing else inside it. And it never displays at all.
Well, if TileMap uses shared data, and more than one thread is trying to use this data, there may be a race condition going on.

Although you say it doesn't draw... Are you sure the thread is starting? Check the return value of CreateThread(). If it returns 0, print GetLastError() to get the error code.
It probably isn't a bad idea to check the OGL documentation, either.
Well I'll see if I can find anything in the documentation. GetLastError() returned 0 for me. So I'll keep hunting.

EDIT : I just changed the draw function to even just draw a shape to see if that'd draw. But still no luck does nothing.

EDIT 2 : I just threw a printf in there to see if it would come up in the console for debugging. It prints once during creation and never happens again. That help narrow it down?
Last edited on
Unless something has changed in the last couple years, OpenGL is not cross-thread capable.

In other words, you can only call render functions in the same thread that initializes OpenGL.

DX9 has the same limitation, and perhaps even 10 and 11, but I haven't used those versions.
Yeah, I thought it was something like that.
Awesome - thanks for that mate. I actually read it in the documentation late last night, but they way they worded it was a little fuzzy. Thanks for clarifying it for me.
Topic archived. No new replies allowed.