Mutex is totally independent with thread priority. Am I right ??? I am aware with priority inversion type problems.
But here, things are totally different on MBED OS.
I created two threads with different priority level and one mutex object.
But When low priority thread lock my code and if at same time high priority thread invoke then MUTEX can not protect my Critical section code, High priority thread enter into Critical section block (which is locked by low priority thread).
Try to run following code and look the output on serial terminal
#include “mbed.h” //MBED LIBRARY
Mutex M_LOCK; // Create MUTEX OBJECT -class = MUTEX
//Create Two Thread
Thread t2(osPriorityLow); //Create Thread with high priority
Thread t3(osPriorityHigh); //Create Thread with low priority
void common_function(const char *name, int state)
{
printf(“Thread arrive at door %s: %d\n\r”, name, state);
M_LOCK.lock(); //After arrive lock the code---------------------------LOCK THE BLOCK
printf("This Thread lock the code %s: %d\n\r", name, state);
wait(0.5); //sleep
M_LOCK.unlock(); //After completing task unlock the code ------------- UNLOCK THE BLOCK
printf("Thread cross & unlock %s: %d\n\r", name, state); //OUTSIDE CODE BLOCK ---------
}
void test_thread(void const *args)
{
while (true) {
common_function((const char *)args, 0);
ThisThread::sleep_for(500);
common_function((const char *)args, 1);
ThisThread::sleep_for(500);
}
}
int main()
{
t2.start(callback(test_thread, (void *)“Th 2”));
t3.start(callback(test_thread, (void *)“Th 3”));
test_thread((void *)"Th 1"); // DIRECT CALL via main thread
}
And you clearly see even mutex lock High priority thread cross /break that barrier.
Why ??, Bug ??? or Features ???
SERIAL TERMINAL OUTPUT
Jaydeep shah - radhey04ec@gmail.com