Sign in to follow this  
DarkHacker

Thread Help

Recommended Posts

DarkHacker    128
What I'm trying to do is add a seperate thread for loading my map data so the program can display a loading screen and later on I wanted to use it for making map changes seamless. Since I'm going to be using the thread in my LoadMap function only I thought I'd go about doing this:
void LoadMap(string fileName)
{
    //CreateThread

    //ExecuteDataLoadingFunction with the fileName passed to it in another thread so the rendering loop can continue

     //Then after the data is loaded start creating the GL Textures and VBOs based on the data loaded in the thread
}


How would I go about doing this using SDL's threading functions? I've tried on my own based on the docs at SDL's official page but it crashes my program.

Share this post


Link to post
Share on other sites
ChrisTheAvatar    134
Here is a start for you,

Im assuming your using C++ (not C#) and on a Win32 platform:
you need to include <process.h>

This call will make a new thread:

_beginthread(void process(void * arg),0, void * arg); //create new thread

This function will create a new thread and begin processing immediately on the sub routine you pass to it (See first argument) you also may pass arguments to the function you want the thread to process through the 3rd argument of _beginthread. Its been sometime since ive used this so I cant recall what the second argument is.

I believe that it returns an integer value representing the threads handle. You will have to read more about this function at Microsofts website. Threading calls are much more clearer on Linux, so if you do happen to be developing for Linux please email me at clapay@fantasyrealmonline.com and I will provide you more information on Linux multithreading, I can also provide a wealth of information with MT for C#.

Additionally if you have not read up on multithreading I strongly suggest you do so. As soon as you introduce multiple threads into a program you add a number of new problems that you need to take care of and provide error handeling for. Heres some key words for you to lookup, I do not have time to go into great detail now but to give you an idea:

Deadlock
Critical Section Problem
Mutexs / Semaphores
Public Variable Access

In my game I am using threads for a similar purpose I read blocks of a map with one thread while another handles character animation and moving the map so by the time the animation is over the next block is read from the disk and in memory.

Chris The Avatar
Professional Software Developer

Share this post


Link to post
Share on other sites
DarkHacker    128
Thanks!

I got a thread working now (it doesn't crash anymore), I'll report back when I have it working like I wanted and post code for other people who are looking to add another thread using SDL.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this