6 years, 5 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

6 years, 5 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

Accepted Answer

I should have searched specifically on RTOS Threads. I only searched on member initialization. Working from the example in your link, I was able to get a this working. Thanks.

posted by David G 18 Feb 2014
6 years, 5 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 julian C 17 Aug 2014

You would need to use a static member function, that will work, but no regular member functions.

posted by Erik - 17 Aug 2014