1
System / Re: Multiple argument functioned thread
« on: September 04, 2012, 12:44:47 am »Quote from: eXpl0it3r
I'm assuming you're refering to SFML 2.Yeah sorry i meant 2.0. i think i might be able to do what i need with a new class or possibly a struct, ill check it out unless the std::bind works for what i need
I guess there are many ways to achive this.
Since you can declare which type you want to pass, you can create the type you want, so it could be your own struct or your own class or just an std::vector, etc...
Another way is to have the variable in the same scope (probably global scope in your case), which needs quite a bit more attetion so you don't run into race conditions. Also keep in mind global variables aren't such a nice code design. For the global scope problem you could make the function a member of a class and then use the member variables (you'll still have to make sure that there are no race conditions).
The two easiest solutions are:sorry im a bit of a novice programmer in c++ and using the sfml librarys, but im guessing the struct will just gather my 4 parameters into one so it would look something like
1. Use C++11 or boost or tr1sf::Thread thread(std::bind(&f, a, b, c, d));
2. If you can't use anything of solution 1., then gather the four parameters into onestruct Params
{
...;
...;
...;
...;
};
Params p = {a, b, c, d};
sf::Thread thread(&f, p);
Code: [Select]
struct Param
{
GamePlayer player;
DBManager dbManager;
CameraManager camManager;
};
then for the function im calling for the thread it would beCode: [Select]
void TheThreadFunction(Param p)
{
//code for thread belongs here
}
and finally the call to my thread would be (within int main())Code: [Select]
Param newParam {activePlayer, activeDBManager, activeCamera}
sf::Thread myThread(&TheThreadFunction, newParam);
myThread.launch();
where activePlayer, activeDBManager, and activeCamera are already set instances of GamePlayer, DBManager, and CameraManagerdoes this look right?
also what it looks like i just did there the std::bind looks like it basically took care of that for me. Ill play around with it and see how either of those options work out. Thank you