6 years, 9 months ago.

trylock inside event callback not working?

Why does trylock always return 1 in the following code? I thought interruptIn.fall with a callback to eventqueue made it possible to use mutexs?

---

  1. include "mbed.h"

static EventQueue buttonQueue(/* event count */ 8 * EVENTS_EVENT_SIZE); Thread button_thread(osPriorityLow);

InterruptIn btn(p20);

Mutex btn_m;

void button_finnish() { do some stuff

printf("Button is finnished"); btn_m.unlock();

} void button_press() { if (!btn_m.trylock()) { ERROR trylock always return 1 printf("Button is already pressed"); return; }

printf("DEBUG trylock should now be false / 0 : %d ",(int)btn_m.trylock());

printf("Button is pressed");

buttonQueue.call_in(5000, button_finnish); do some stuff printf("Disabling button for 5s (mutex is locked)"); }

int main() {

btn.fall(buttonQueue.event(button_press)); button_thread.start(callback(&buttonQueue, &EventQueue::dispatch_forever));

}

Be the first to answer this question.