Mbed-os: C027 - Thread example not working

Created on 8 Dec 2016  路  10Comments  路  Source: ARMmbed/mbed-os

Description

- Type: Bug

Bug

Target
C027

Toolchain:
mbed online compiler


Question

How to start running a thread with Thread::start() ?

The example in the docs for the thread (from the Thread.h) is not working at all.
Compiled on the mbed online compiler with the newest version of mbed-os imported directly from github.

#include "mbed.h"
#include "rtos.h"

Thread thread;
DigitalOut led1(LED1);
volatile bool running = true;

// Blink function toggles the led in a long running loop
void blink(DigitalOut *led) {
while (running) {
*led = !*led;
Thread::wait(1000);
}
}
// Spawns a thread to run blink for 5 seconds
int main() {
thread.start(led1, blink);
Thread::wait(5000);
running = false;
thread.join();
}

Compiler error number: 304
Compiler error description: No instance of overloaded function "rtos::Thread::start" matches the argument list "thread.start(led1, blink);"

rtos bug

Most helpful comment

thread.start(led1, blink);

Instead of the above, you'll need to use a reference like this:

thread.start(blink, &led1);

Though, the above is deprecated in favor of using callbacks like this:

thread.start(callback(blink, &led1));

Thread::start should be passed a callback. The order of parameters to the callback is either:
callback([object to invoke member function], [member function])
OR
callback([function], [pointer argument to function])

https://docs.mbed.com/docs/mbed-os-api/en/mbed-os-5.3/api/classrtos_1_1Thread.html

All 10 comments

I can confirm it does not compile. I suspect there were changes to Thread class, that have not been reflected to the example provided in the header file. It should be using start with a callback object as a argument,

cc @geky

Seems like this is related - https://github.com/ARMmbed/mbed-os/issues/3203. We should fix both

thread.start(led1, blink);

Instead of the above, you'll need to use a reference like this:

thread.start(blink, &led1);

Though, the above is deprecated in favor of using callbacks like this:

thread.start(callback(blink, &led1));

Thread::start should be passed a callback. The order of parameters to the callback is either:
callback([object to invoke member function], [member function])
OR
callback([function], [pointer argument to function])

https://docs.mbed.com/docs/mbed-os-api/en/mbed-os-5.3/api/classrtos_1_1Thread.html

Thanks for resolving this issue quickly, tested it on the C027 and the KL25Z, it works on the KL25Z but not on the C027 (the LED doesn't blink at all). Tested on the C027 with three test cases:
First case used the onboard LED, second case with an external LED connected to P0_1, third case with an external LED connected to P1_20.
@0xc0170 @sarahmarshy

Forgot to say that including rtos.h is not necessary since it is already a part of mbed-os.

@jsimunic The test example to reproduce the problem is the same as above?

cc @mazgch

@0xc0170 Well I used the example above with the changes that @sarahmarshy did in #3469 in the mbed online compiler with mbed-os (tag:mbed_lib_rev133).
Here it is:

#include "mbed.h"
#include "rtos.h"

Thread thread;
DigitalOut led1(LED1);
volatile bool running = true;

// Blink function toggles the led in a long running loop
void blink(DigitalOut *led) {
    while (running) {
    *led = !*led;
    wait(1);
    }
}
// Spawns a thread to run blink for 5 seconds
int main() {
    thread.start(callback(blink, &led1));
    wait(5);
    running = false;
    thread.join();
}

In all of the test cases I changed only line 5 where we instantiate the DigitalOut object:
First case:
DigitalOut led1(LED);
Second case:
DigitalOut led1(P0_1);
Third case:
DigitalOut led1(P1_20);

@jsimunic Did this get resolved or clarified?

@sg- Still nothing, didn't try to contact guys from u-blox, haven't tried out the test cases on a LPC1768 (the MCUs are the same on C027 and a LPC1768 only different package, right?) but I will in the following weeks when I gain access to a LPC1768.
Just to mention, the rtos example for mbed 2 works on C027, this is probably an issue with mbed 5 (mbed-os) only.

GitHib issue review: Closed due to inactivity. Please re-file if critical issues found.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ashok-rao picture ashok-rao  路  4Comments

toyowata picture toyowata  路  4Comments

1domen1 picture 1domen1  路  3Comments

rbonghi picture rbonghi  路  3Comments

DuyTrandeLion picture DuyTrandeLion  路  3Comments