Arm Mbed OS support forum

Does Mutex lock consider thread priority level?

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 :angry:

#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 :angry:

Jaydeep shah - radhey04ec@gmail.com

Hello Jaydeep,

Because only a part of the shared function is protected by a mutex the printouts are misleading. To check whether only one thread is using the protected part at a time modify the function for example as below:

void common_function(const char* name, int state)
{
    ThisThread::sleep_for(500);
    M_LOCK.lock();
    printf("---------------------------\r\n");
    printf("%s locked the code\t: %d\r\n", name, state);
    ThisThread::sleep_for(500);
    printf("%s unlocked the code\t: %d\r\n", name, state);
    printf("---------------------------\r\n");
    M_LOCK.unlock();
    ThisThread::sleep_for(500);
}

Then the printout should be similar to this:

...
---------------------------
Th 3 locked the code	: 0
Th 3 unlocked the code	: 0
---------------------------
---------------------------
Th 1 locked the code	: 0
Th 1 unlocked the code	: 0
---------------------------
---------------------------
Th 3 locked the code	: 1
Th 3 unlocked the code	: 1
---------------------------
---------------------------
Th 2 locked the code	: 0
Th 2 unlocked the code	: 0
---------------------------
---------------------------
Th 3 locked the code	: 0
Th 3 unlocked the code	: 0
---------------------------
---------------------------
Th 1 locked the code	: 1
Th 1 unlocked the code	: 1
---------------------------

...

As you can see, only one thread is using the protected section at a time.

Best regards, Zoltan

Thanks for reply dear.
But have you used different priority level of two threads ?? In normal case it works fine.

As I mentioned I had used all three threads with different priority .

Can you please explain how print misleading me ?

Hello Jaydeep,

I copy & pasted your code into my editor and modified only the common_function. By the way, your code did not compile with Mbed OS 6 because of calling

wait(0.5); //sleep

in the protected section.

Can you please explain how print misleading me ?

I think when one thread is executing the protected section it doesn’t mean the other threads are completely blocked until it leaves that section. It only prevents them to enter the protected section as demostrated in my printout.

Best regards, Zoltan

Thanks dear;

I am assuming printf() after unlock misleading me.

You should look at the manual for “RTX,” that’s the RTOS mbed is built on. I dug into it once, and I think the mutexes have “priority inheritance,” that is, a thread owning a mutex that a thread of higher priority wants will “inherit” the priority of the thread sleeping on the desired mutex

1 Like