-1

i'm just starting to learn coding and i have a problem right now... I searched on the forum for a similar one but i didnt find any...

I have class (Scheduler) and a template function in this class (function)

from the default constructor of this class i try to execute this function within a thread

im not sure i was very clear so i give you the lines :

the errors :

-static assertion failed: std::thread arguments must be invocable after conversion to rvalues

-error: no type named 'type' in 'struct std::thread::_Invoker<std::tuple<void (Scheduler::*)(int&, Temperature&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, char), Scheduler*, int, Temperature, const char*, char> >::__result<std::tuple<void (Scheduler::*)(int&, Temperature&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, char), Scheduler*, int, Temperature, const char*, char> >'

the function (which is in the public part of my class Scheduler) :

template<class T, class S>
     void function(T& p_data,S& obj, const std::string p_file, char p_type)
     {
         for (int i = 0; i<10; i++){
                    Sleep(3500);

                    p_data = obj.getData();
                    serv.fileWrite(p_data, p_file, p_type);
                    serv.consoleWrite(m_Tmp, 'T');
                    serv.consoleWrite(m_Prs, 'P');
                    serv.consoleWrite(m_Hmy, 'H');
                    serv.consoleWrite(m_Light, 'L');
                    std::cout << "--------------------" << std::endl;
                }

     }

the thread (which is in the default constructor) :

thread t1(&Scheduler::function<int,Temperature>, this, m_Tmp, objTemperature, "Logs/Temperature.log", 'T');

Here it is ... I'm not seeing what i am doing wrong...

πάντα ῥεῖ
  • 85,314
  • 13
  • 111
  • 183
RICOLA
  • 25
  • 4
  • 1
    Pass `&Scheduler::function` without the first ampersand. Also, is `function` a static function? – Wais Kamal Oct 12 '21 at 18:15
  • I'm not sure what is a static function... i did not define it this way (if it's not the default one) the ampersand changed nothing i already tried – RICOLA Oct 12 '21 at 18:21
  • You may want to have a look at [this answer](https://stackoverflow.com/a/15235626/6649786) to better understand static functions. – Wais Kamal Oct 12 '21 at 18:24
  • 1
    Check `std::ref` if you want to pass variables by reference into thread function. – rafix07 Oct 12 '21 at 18:26
  • It worked as you told me rafix07 thank you very much to all of you – RICOLA Oct 12 '21 at 18:31

1 Answers1

0

While it's not strictly the only way to go about it, wrapping the entrypoint of the thread in a lambda generally makes things a lot more straightforward:

thread t1([this, objTemperature]{
  function(m_Tmp, objTemperature, "Logs/Temperature.log", 'T');
});

Frank
  • 18,728
  • 1
  • 23
  • 49