vendredi 22 juillet 2016

Instantiate std::thread with a function that expects a function as a parameter

I have a function that expects another function as a parameter, as follows:

void ModbusAgent::poolingThread(int position, std::function<int(int, uint16_t*)> readFunction)

In this way, I'm creating a bind as follows:

std::function<int(int, uint16_t*)> readFunction = std::bind(&ModbusAgent::readRegister, this, std::placeholders::_1, std::placeholders::_2);

When I call my the poolingThread function with the following line, it works:

this->poolingThread(position, readFunction);

However, the following:

std::thread(&ModbusAgent::pool, this, position, readFunction);

I get an error:

In file included from /usr/include/c++/5/thread:39:0,
                 from /home/fccoelho/Code/ivsn_modbus_lib/Test/include/modbus_agent.hpp:8,
                 from /home/fccoelho/Code/ivsn_modbus_lib/Test/src/modbus_agent.cpp:1:
/usr/include/c++/5/functional: In instantiation of ‘struct std::_Bind_simple<std::_Mem_fn<void (ModbusAgent::*)(int, ModbusAgent::RegType)>(ModbusAgent*, int, std::function<int(int, short unsigned int*)>)>’:
/usr/include/c++/5/thread:137:59:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (ModbusAgent::*)(int, ModbusAgent::RegType); _Args = {ModbusAgent*, int&, std::function<int(int, short unsigned int*)>&}]’
/home/fccoelho/Code/ivsn_modbus_lib/Test/src/modbus_agent.cpp:147:92:   required from here
/usr/include/c++/5/functional:1505:61: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (ModbusAgent::*)(int, ModbusAgent::RegType)>(ModbusAgent*, int, std::function<int(int, short unsigned int*)>)>’
       typedef typename result_of<_Callable(_Args...)>::type result_type;
                                                             ^
/usr/include/c++/5/functional:1526:9: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (ModbusAgent::*)(int, ModbusAgent::RegType)>(ModbusAgent*, int, std::function<int(int, short unsigned int*)>)>’
         _M_invoke(_Index_tuple<_Indices...>)

How can I solve that?

Aucun commentaire:

Enregistrer un commentaire