Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 9 months ago.
C++ member initialization list for Thread::Thread constructor within a class?
I want to initialize a Thread object within a class, Test, to execute a member function of Test. The code that I've tried generates a compiler error 289 on line 20. I understand that this means that no constructor of Thread::Thread() matches the argument list of Test::Test(...). My code uses the member initialization list form of member initialization.
I'm not sure if my syntax is incorrect or if there is something else I am missing. Does anyone know how I might do this? It seems that this should be possible, but I'm not seeing how to do it.
#include "mbed.h" #include "rtos.h" class Test { public: Test( PinName pin ); protected: void _isr( void ); void _Tfunc(void const *args); InterruptIn _interrupt; Thread _threadObj; }; // How to initialize Thread::_threadObj object to run the member function Test::_Tfunc()?? Test::Test( PinName pin ) : _interrupt( pin ), _threadObj( _Tfunc ) { _interrupt.rise( this, &Test::_isr ); } void Test::_isr( void ) { _thread.signal_set( 0x01 ); } void Test::_Tfunc(void const *args) { while( true ) { Thread::signal_wait( 0x01 ); } } int main() { Test testObj( p14 ); while(1) { } }
2 Answers
10 years, 9 months ago.
Hello,
please check the contructor for Thread class which is definition is in mbed RTOS:
Thread::Thread(void (*task)(void const *argument), void *argument, osPriority priority, uint32_t stack_size, unsigned char *stack_pointer)
Here's an example how to initialize it properly: https://mbed.org/forum/mbed/topic/4388/ . I found some other examples, just look for thread on mbed. Some of those have different parameters, which indicates it has changed, so be aware not all examples are up to date.
Regards,
0xc0170
10 years, 9 months ago.
_Tfunc is here not a regular function, but a member function. And Thread can only use regular functions (and I assume also static member functions).
Hi Erik,
as per your comments something like this will not work ?
VarStore::VarStore() { VarCounter=0; Thread thread(Worker);
}
with Worker as a private member function
defined as
void Worker(void const *args);
I get the compiling error
Error: No instance of constructor "rtos::Thread::Thread" matches the argument list in "VarStore.cpp", Line: 10, Col: 19
Thanks.
Julian
----
this won't work neither.Moving the init code to main to test if I can initiate the thread out of the constructor.
VarStore Store;
Thread thread(Store.Worker,NULL,osPriorityNormal,DEFAULT_STACK_SIZE,NULL);
I get
Error: A pointer to a bound function may only be used to call the function in "main.cpp", Line: 26, Col: 26
posted by 17 Aug 2014