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

Author Topic: Error when using threads in a class  (Read 5238 times)

0 Members and 1 Guest are viewing this topic.

SFJ

  • Newbie
  • *
  • Posts: 5
    • View Profile
Error when using threads in a class
« on: March 24, 2015, 05:46:54 pm »
Hi,

I am trying to call a function inside a class, which creates a static thread:

#include <cstdio>
#include <conio.h>
#include <SFML/System.hpp>

class MY_CLASS
{
        public:
                void firstFunc()
                {
                        int counter = 0;
                        while(1)
                        {
                                printf("Hello from firstFunc() %d\n", counter);
                                counter++;
                                sf::sleep(sf::Time(sf::seconds(1)));
                        }
                }
                void firstFuncCaller()
                {
                        static sf::Thread firstFuncThread(&MY_CLASS::firstFunc);
                        firstFuncThread.launch();
                }
};

int main()
{
        MY_CLASS myClassInstance;
        int counter = 0;
       
        myClassInstance.firstFuncCaller();
        while(1)
        {
                printf("Hello from main() %d\n", counter);
                counter++;
                sf::sleep(sf::Time(sf::seconds(1)));
        }
       
        return 0;
}

But every time I compile it, I get the following error:

In file included from c:\mingw\include\sfml\system\thread.hpp:193:0,
                 from c:\mingw\include\sfml\system.hpp:40,
                 from srcs/consoleTests/threadTest/threadTest.cpp:3:
c:\mingw\include\sfml\system\thread.inl: In instantiation of 'void sf::priv::ThreadFunctor<T>::run() [with T = void (MY_CLASS::*)()]':
srcs/consoleTests/threadTest/threadTest.cpp:42:1:   required from here
c:\mingw\include\sfml\system\thread.inl:39:25: error: must use '.*' or '->*' to call pointer-to-member function in '((sf::priv::ThreadFunctor<void (MY_CLASS::*)()>*)this)->sf::priv::ThreadFunctor<void (MY_CLASS::*)()>::m_functor (...)', e.g. '(... ->* ((sf::priv::ThreadFunctor<void (MY_CLASS::*)()>*)this)->sf::priv::ThreadFunctor<void (MY_CLASS::*)()>::m_functor) (...)'
     virtual void run() {m_functor();}
                         ^

What do I have to change in my code to let it compile properly?
Thanks for any help.

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Error when using threads in a class
« Reply #1 on: March 24, 2015, 06:03:43 pm »
The thread needs an object on which the member function can be called, hence:

static sf::Thread firstFuncThread(&MY_CLASS::firstFunc, this);

doc: http://www.sfml-dev.org/documentation/2.2/classsf_1_1Thread.php#aa9f473c8cbb078900c62b1fd14a83a34
SFML / OS X developer

SFJ

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Error when using threads in a class
« Reply #2 on: March 24, 2015, 06:07:59 pm »
Ahh, this works now,  thank you very much!

SFJ

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Error when using threads in a class
« Reply #3 on: March 24, 2015, 06:21:26 pm »
Wait, but what do I have to write when I use additional arguments for "firstFunc()", for example "firstFunc(int counterStart)"? I tried "static sf::Thread firstFuncThread(&MY_CLASS::firstFunc, 1234, this);" and "static sf::Thread firstFuncThread(std::bind(&MY_CLASS::firstFunc, 1234), this);", but none of them work (they both produce hundreds of lines of errors).

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Error when using threads in a class
« Reply #4 on: March 24, 2015, 06:51:33 pm »
Just read the doc. It's explained there.
Laurent Gomila - SFML developer

SFJ

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Error when using threads in a class
« Reply #5 on: March 24, 2015, 07:36:05 pm »
OK, thanks for your answer and sorry for not reading the doc. :)
For everybody else, this is my solution: "static sf::Thread firstFuncThread(std::bind(&MY_CLASS::firstFunc, this, 1234));"