2017-06-25 13:07:33

Hello,
I have a question about SFML library. How can I force it that it will open thread with more than one parameter?

If you want to contact me, do not use the forum PM. I respond once a year or two, when I need to write a PM myself. I apologize for the inconvenience.
Telegram: Nuno69a
E-Mail: nuno69a (at) gmail (dot) com

2017-06-25 13:24:43

That should be obvious. As far as I can see the sf::Thread constructor accepts as many arguments as you need. Meaning that you can give it the function to call as the first parameter and all required arguments after that. For more examples go here.
Best Regards.
Hijacker

2017-06-25 14:34:33

I need call function with one or more argument. I did read about the std::bind, but I don't know what i can implement it in my program.

2017-06-25 17:33:35

The link I posted above explains everything, and as far as I can see you don't need std::bind for that.

Best Regards.
Hijacker

2017-06-25 18:56:57

I know it because I can't forward more than 3 parameters, so I need std::bind.

2017-06-25 20:16:04

There are actually multiple aproaches you could take to reach your goal here, and all of these are shown in the link above. The first one would be to use a lambda which then calls the function you need with all the arguments you require, or, as you already said, using std::bind, which is absolutely easy too.

// with std::bind
void func(std::string, int, double)
{
}

sf::Thread thread(std::bind(&func, "hello", 24, 0.5));

So what's the actual problem you're facing here?
As the link above already says, if you're using C++ and a comparably new compiler, use the native C++11 threads instead, they are safer and more efficient.
Best Regards.
Hijacker

2017-06-25 20:19:50

I have a function with 4 arguments. int number, float number, float number, float number. These arguments haven given when program works. Ped Identificator, float x, float y, and float z destination coordinates. I cant compile it, maybe because I don't write void function(std::bind)..., I will try that.

2017-06-25 22:22:25

My function is freezing my program.

2017-06-26 10:29:20

That's not SFML's problem, but you provoking a deadlock or lifelock.
Let's take this example:

void print_sum(int v1, int v2, int v3, int v4)
{
  std::cout << (v1 + v2 + v3 + v4) << endl;
}
int main()
{
  int a = 1;
  int b = 2;
  int c = 4;
  int d = 8;
  sf::Thread t(std::bind(&print_sum, a, b, c, d));
  t.launch();
}

This should work fine and print you the sum of the 4 variables a, b, c and d. If your program is written correctly and it freezes anyway you're accessing variables and/or functions from inside your threaded function which gets accessed from the main thread too and therefore block each other. In this case you'll have to make your program thread safe. If you don't know what that means, you probably shouldn't go with threads anyway or read yourself through thread example on your own first.
Best Regards.
Hijacker

2017-06-30 21:40:49 (edited by visualstudio 2017-06-30 21:42:28)

hello,
you can use structures instead of more parameters:

typedef struct a {
int a;
int b;
int c;
int d;
};

void some_func(a& s)
{
cout<< s.a+s.b+s.c+s.d;
}

int main()
{
a* as=new a;
sf::thread t(&some_func, &as);
t.launch();
delete as;
}

please note, i've didnt test this code
also you can use mutexes to lock things (i havent delt with sfml's thread api)

2017-06-30 23:26:10

I have my function in class, and I cant user std::bind(&Class::function, parameters); because i receive this error:
/usr/include/c++/7.1.1/functional:588:2: note: candidate: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const volatile [with _Args = {_Args ...}; _Result = _Result; _Functor
= void (Ped::*)(int, float, float, float); _Bound_args = {int, float, float, float}]
  operator()(_Args&&... __args) const volatile
  ^~~~~~~~
/usr/include/c++/7.1.1/functional:588:2: note:   template argument deduction/substitution failed:

2017-07-01 17:45:29

use this instead:

std::bind(&classname, &func, parameters);

2017-07-01 19:16:09

It doesnt work.
ped.cpp:181:36: error: expected primary-expression before ‘,’ token thr = new sf::Thread(std::bind(&Ped, &goToCoords, d, a_ped[d].x, a_ped[d].y - cy, a_ped[d].z));
ped.cpp:181:39: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.  Say ‘&Ped::goToCoords’ [-fpermissive] thr = new sf::Thread(std::bind(&Ped, &goToCoords, d, a_ped[d].x, a_ped[d].y - cy, a_ped[d].z));
                                       ^~~~~~~~~~