Threads and a confused programmer [solved]

Started by
8 comments, last by Nitage 18 years, 6 months ago
OK the time time as come to learn aboot threads, since it looks like my emulator will be impossible to do without them. Here's what my code looks like:

DWORD ThreadId;
HANDLE ThreadHndl;

DWORD WINAPI ThreadProc(LPVOID lpParam) {
//do stuff
return 0;}

INT WINAPI WinMain(stuff){
//window setup
ThreadHndl = CreateThread(NULL,0,ThreadProc,NULL,0,&ThreadId);
//start message pump
GameLoop{}
//user exits
//delete everything
}



My understanding was that thread functions ran alongside everything else. However the ThreadProc function is only called when the thread is created. So what's wrong? What do I need to do so both threads(WinMain and ThreadProc) run together? [Edited by - Scet on October 10, 2005 11:10:21 AM]
Advertisement
Does your ThreadProc() contain a loop that continues running for a long time? Because that is often what you have in such a thread function. You'll probably want to have a boolean variable, something like gRunning, that the ThreadProc() function checks in a while loop. And from the main thread, you set gRunning equal to false when you want the secondary thread to quit. The while loop will terminate, the function will finish up any code that it needs to do, and then the function will return. Close the handle to the thread, and you're done.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
you are right! it is called when it is created.
what you have to do is to create some sort of loop on ThreadProc and you will see it ir running alongside winmain. The way it is, it does nothing so it the thread dies immediatelly.
* leandro.
OK I got it sort of working:

bool Done=0;DWORD ThreadId;HANDLE ThreadHndl;DWORD WINAPI ThreadProc(LPVOID lpParam) {while(!Done){//do stuff}return 0;}INT WINAPI WinMain(stuff){//window setupThreadHndl = CreateThread(NULL,0,ThreadProc,NULL,0,&ThreadId);//start message pumpGameLoop{}//user exitsDone=1;//delete everything}


It runs, but now my FPS is 20 [sad].
A quick way to remedy that is to Sleep(10) or something like that. How often do you want the second thread to go through the loop? If it needs to be perfectly synchronized with the main loop, then you might want to rethink threading. However, if it doesn't need to be, then determine how long you're willing to sleep between each iteration of the loop, and put in a Sleep command. Anything will probably get your frame rates up to normal speed, though, most likely even Sleep(0).
DWORD WINAPI ThreadProc(LPVOID lpParam){  while(!Done)  {    //do stuff    Sleep(10);  }  return 0;}
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
Alright it works now, thanks [smile]. See the thing is on the GBA the processor,timers,LCD,DMA,etc.. all have their own chips and I can't keep doing everything in a linear game loop by just checking for VBlank.
I don't understand - you're using the Win32 API on a GBA?
Quote:Original post by Nitage
I don't understand - you're using the Win32 API on a GBA?


Quote:Original post by me
OK the time time as come to learn aboot threads, since it looks like my emulator will be impossible to do without them.

Quote:Original post by Nitage
I don't understand - you're using the Win32 API on a GBA?


He is writing an emulator which purpose is to emulate GBA.
Sorry, didn't see that.

This topic is closed to new replies.

Advertisement