Hello,
I have a question about SFML library. How can I force it that it will open thread with more than one parameter?
Telegram: Nuno69a
E-Mail: nuno69a (at) gmail (dot) com
You are not logged in. Please login or register.
AudioGames.net Forum → Developers room → SFML question
Hello,
I have a question about SFML library. How can I force it that it will open thread with more than one parameter?
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
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.
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
I know it because I can't forward more than 3 parameters, so I need std::bind.
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
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.
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
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)
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:
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));
^~~~~~~~~~
AudioGames.net Forum → Developers room → SFML question
Generated in 0.077 seconds (15% PHP - 85% DB) with 11 queries