Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
6 years, 10 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?
---
- 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));
}