Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Multiple argument functioned thread  (Read 10846 times)

0 Members and 1 Guest are viewing this topic.

crumpetxxix

  • Newbie
  • *
  • Posts: 9
    • View Profile
Multiple argument functioned thread
« on: September 03, 2012, 11:09:21 pm »
So with in sf::Thread you can only pass a thread through a function with no or just 1 argument.  For my game, I have a function which checks whether or not a new background tile needs to be loaded or not, then loads it.  Although this function requires four arguments.  So i know that i cant make this function into a thread so i was wondering how i would get around this limitation

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Multiple argument functioned thread
« Reply #1 on: September 03, 2012, 11:18:14 pm »
I'm assuming you're refering to SFML 2.

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). ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Multiple argument functioned thread
« Reply #2 on: September 03, 2012, 11:35:29 pm »
The two easiest solutions are:

1. Use C++11 or boost or tr1
sf::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 one
struct Params
{
    ...;
    ...;
    ...;
    ...;
};

Params p = {a, b, c, d};
sf::Thread thread(&f, p);
Laurent Gomila - SFML developer

crumpetxxix

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Multiple argument functioned thread
« Reply #3 on: September 04, 2012, 12:44:47 am »
Quote from: eXpl0it3r
I'm assuming you're refering to SFML 2.

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).
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
The two easiest solutions are:

1. Use C++11 or boost or tr1
sf::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 one
struct Params
{
    ...;
    ...;
    ...;
    ...;
};

Params p = {a, b, c, d};
sf::Thread thread(&f, p);
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
Code: [Select]
struct Param
{
    GamePlayer player;
    DBManager dbManager;
    CameraManager camManager;
};
then for the function im calling for the thread it would be
Code: [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 CameraManager

does 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
« Last Edit: September 04, 2012, 12:46:40 am by crumpetxxix »

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Multiple argument functioned thread
« Reply #4 on: September 04, 2012, 06:37:02 pm »
Yes, that's essentially the solution. However you'd probably want to pass either pointers or references rather than objects (to not copy everything and possibly lose changes being made).

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Multiple argument functioned thread
« Reply #5 on: September 04, 2012, 07:39:45 pm »
Yes, that's essentially the solution. However you'd probably want to pass either pointers or references rather than objects (to not copy everything and possibly lose changes being made).
Then again you need to pay attention not trying to change anything on those objects in two diffrent threads (= possible race condition) and if so to lock the right etc. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything