2 years, 12 months ago.

Mbed OS 5, RTX, RTOS, Correct way to create a new Thread?

Sorry, I am not finding an answer to this. In the few mbed-os examples that I can find, new threads are created using this form:

Thread t1(thread1,(void *)"Th 1");

And you can dive in and view the constructors in Thread.h. Makes sense, except there is a note for each version of the constructor that seems to indicate this method for spawning threads should not be used.


@deprecated Thread-spawning constructors hide errors and may lead to complex program state when a thread is declared.

The explicit Thread::start member function should be used to spawn a thread.

Can someone explain, what is the correct way to start a new Thread?


Comment on this question

4 Answers

2 years, 12 months ago.


this is ctor to be used:

Thread t1(); //using default parameters

I am getting a compiler overload mismatch error using start:

//the old way
//Thread th1(HouseKeeping, NULL, osPriorityNormal, (DEFAULT_STACK_SIZE * 2.25));

//the new way
    Thread th1(osPriorityNormal, (DEFAULT_STACK_SIZE * 2.25), NULL);

Error: No instance of overloaded function "rtos::Thread::start" matches the argument list in "main.cpp", Line: 2714, Col: 10


posted by Kevin Braun 26 Aug 2016

I found the problem. You also need to remove the argument list from the call_back routine as well:

//old way
void HouseKeeping(void const *argument) {
//new way
void HouseKeeping() {


posted by Kevin Braun 26 Aug 2016
2 years, 11 months ago.

The reason is that if the malloc for the stack fails, the constructor has no way to indicate this failure. This is particularly tricky for statically allocated threads (static constructors), where the constructor gets called way before the main function is executed.

Normally, an exception would be thrown for a situation like this, however, mbed does not have C++ exceptions enabled, and constructors can't return values. So the `start` method returns a status value, which needs to be checked for an error state.


2 years, 11 months ago.

How do I pass arguments to the function?

void some_function(int i);

int main(void) {
    int a = 2;

    Thread t1(); 
    t1.start(some_function, a);  // this won't work

Short answer: You can't!

Please open a ticket for it in our Github repository:

posted by Vincent (pan-) Coubard 31 Aug 2016
9 months, 2 weeks ago.

You can pass arguments using the callback feature.

You need to log in to post a question