• Advertisement
Sign in to follow this  

Threads and a confused programmer [solved]

This topic is 4486 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 setup
ThreadHndl = CreateThread(NULL,0,ThreadProc,NULL,0,&ThreadId);
//start message pump
GameLoop{}
//user exits
Done=1;
//delete everything
}




It runs, but now my FPS is 20 [sad].

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement