Jump to content
  • Advertisement
Sign in to follow this  
Rasmadrak

Passing variables to threads?

This topic is 4400 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

Just a question regarding threads: I was up all night programming an A'star non-tilebased pathfinding (not completely done yet) but I cannot for the life of me figure out how to supply threads with variables...
1) Unit wants pathfinding to A from B, and storing the path in it's stl list "Waypoints".

2) FindPath(A,B,Unit.Waypoints);

3) FindPath(vector3d Start, vector3d End, list<node> &Path)
 { 
 //do preparations here...
 //...
 //then:

 LaunchThread(Start,End,Path);
 }
How can I supply the thread with variables..? I havn't used threads that much (at all, more or less). Also, returning values with threads... how can this be done?

Share this post


Link to post
Share on other sites
Advertisement
What threading library are you using? Different threading systems use different mechanisms to pass arguments.

Share this post


Link to post
Share on other sites
I'm using WINAPI, standard thread creation...


DWORD PathThread;

DWORD WINAPI ThreadFunc(LPVOID pvParam) { //... Code here. };

PathThread = CreateThread(0,
0,
ThreadFunc,
0,
0,
NULL);


...or something similar.


But since I have very limited knowledge about threads, and that I'm at work now - I can't really paste the actual code. It is similar as above thou.

Share this post


Link to post
Share on other sites
To pass arguments to a thread function with the Win32 threading API, you generally aggregate the arguments into a structure and pass a pointer to that structure as the void pointer argument for thread creation. In the function that you use as an argument for the thread you then cast that void pointer back to the struct type and access the members. Ex:

struct Arguments {
int arg1;
int arg2;
std::string arg3;
};

DWORD WINAPI ThreadFunc(LPVOID arg_struct) {
Arguments * args = reinterpret_cast<Arguments *>(arg_struct);

// do stuff
}

// somewhere else
Arguments args = { 0, 5, "Moo! Moo, I say!" };
CreateThread(0, 0, &ThreadFunc, &args, 0, 0);

Share this post


Link to post
Share on other sites
This seems to be exactly what I want!

Some smaller redesigning of the pathfinding class will be needed, but that´s no problem - I think it will only improve it's flexibility!

something like:

struct PathFind
{
bool Done;
node Start,End, Path;
FindPath(...);
};

class unit : public PathFind
{
//...
};


Thank you for your help!!

/Robert

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!