Thread start example

Hi everyone,
I am new in Mbed os and I was reading the documentation when I saw something that I could not understand. In “API references and tutorials”, in condition variable example, this code is used:

void worker_thread()
{
    ...
}

int main()
{
    Thread thread;
    thread.start(worker_thread);
    ...
}

and in EventFlags example they use the code below:

void worker_thread_fun()
{
    ...
}

int main()
{
    Thread worker_thread;
    worker_thread.start(mbed::callback(worker_thread_fun));
    ...
}

My question is why they use “mbed::callback” in the last example and in the other not.
Thanks for your help.

Best Regards

Hi tomrod,

thread.start() takes a callback as its parameter. In the case of the condition variable example the worker_thread() function (because it is a free function, ie not a class method) will be implicitly converted to a callback and thus work. If you wanted to pass a method to the thread.start() then you must explicitly use the callback form.

Hope that answers your question?

Regards
Anna

2 Likes

@AnnaBridge Thanks for your explanation.

Best Regards