6 years, 7 months ago.

wait for interrupt in a non-main thread possible?

The project I am working on will have two thread: 1) the main thread does all the routine work; 2) another thread does nothing most of the time but needs to be activated by certain events such as pressing a key on keypad, run for a few minutes or so then goes back to inactive. In principal I think this is possible but I could not figure out a way to do it.

I first tried _wfi(). That seems put the whole MCU into sleep.

I then tried thread:signal()_set & signal_wait(), it didn't work either.

Edit: the code was updated according to Erik's suggestion and it worked.

include the mbed library with this snippet

#include "mbed.h"
#include "rtos.h"
DigitalOut myled(LED1);
Ticker myticker;
osThreadId mythreadID;

void flipper(void const *args) {
    mythreadID = Thread::gettid();
    while(1) {
        myled = !myled;

void _isr( ) {
    osSignalSet(mythreadID, 0x1);

int main() {
    myticker.attach(&_isr, 1);
    Thread mythread(flipper);
    while (1) {
    //do routine here

Could someone lend me a hand on this? Thanks in advance.

1 Answer

6 years, 7 months ago.

Signal set/wait is the way to go. You can make the thread in main, and only have a global pointer to the thread. I actually am not sure if you are allowed to make a global thread, never done that myself.

I have done this in for example: http://developer.mbed.org/users/Sissors/code/SimpleDMA/file/d3be727fa9d2/SimpleDMA.h (see the parts within #ifdef RTOS_H). Only there it is slightly different since I can get the thread ID, but not a pointer to the thread. So I use a CMSIS-RTOS function which does the same.

Accepted Answer

I opted to use thread ID and it worked. So thanks very much for the tip.

Regarding global thread, as I just tested, though compiler doesn't report error, that doesn't do anything.

posted by Zhiyong Li 18 Dec 2014