I am using trylock_for() and trylock_until() function to protect my critical section. But after sometime my MBED os crash !!! why ??? I have attached image of terminal for more information.
I cant understand how these function work. according to me when first thread enter in to critical section at that time MUTEX flag set for time mentioned in function bracket. After time elapsed mutex automatically unlock that . so next thread can enter into block. Am I right ???
But when next thread (suppose T2) enter at same time all other threads (which are standing at door) also enter in my critical section. Why ? why second thread not set mutex lock for other ?
I attached my code here :
<>
#include “mbed.h”
Mutex Lock;
Thread T1,T2;
void common_block(const char *name,int state)
{
printf(“\n This %s Thread arrive at door with state %d \n”,name,state);
Lock.trylock_until(1); //lock for 3000 ms
printf(“\n Inside the door %s with state %d \n”,name,state);
wait(02); //some how process stuck in wait state
printf(“\n Thread %s is Ouside wait period with state %d \n”,name,state);
Your code doesn’t compile with Mbed OS 6 because of calling the wait function.
Which Mbed OS version do you use?
NOTE: After copy&paste you can prepend and append your code (and serial terminal printouts) with three back-tick characters (```) on separate line to get it formatted.
Hi;
Thanks for quick reply dear.
I am using online MBED platform to write and compile . How can I check version ?I clicked checkbox “update code as per latest library and version” while creating the program
I am using trylock_for() and trylock_until() function to protect my critical section. I cant understand how these function work. according to me when first thread enter in to critical section at that time MUTEX flag set for time mentioned in function bracket. After time elapsed mutex automatically unlock that . so next thread can enter into block. Am I right ???
My understanding of these functions is a bit different:
Tries to lock the mutex. If it succeeds within the specified time frame (interval) the mutex is locked and stays locked until unlocked by calling the unlock function.
Tries to lock the mutex. If it succeeds before the given time point is reached it locks the mutex and the mutex stays locked until unlocked by calling the unlock function.
Usage:
void common_block(const char* name, int state)
{
printf("\n This %s Thread arrive at door with state %d \n", name, state);
if (Lock.trylock_for(100)) { // try to lock the mutex: timeout = 100 ms
printf("\n Inside the door %s with state %d \n", name, state);
ThisThread::sleep_for(2); //some how process stuck in wait state
Lock.unlock();
}
printf("\n Thread %s is Ouside wait period with state %d \n", name, state);
}
Best regards, Zoltan
Honestly, I don’t know why it takes 3 minutes. The comment in the mutex.h says:
/** Try to lock the mutex until specified time
@param abs_time absolute timeout time, referenced to Kernel::get_ms_count()
@return true if the mutex was acquired, false otherwise.
@note the underlying RTOS may have a limit to the maximum wait time
due to internal 32-bit computations, but this is guaranteed to work if the
wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
the lock attempt will time out earlier than specified.
@note You cannot call this function from ISR context.
*/
bool trylock_until(Kernel::Clock::time_point abs_time);
After replacing in your code wait with ThisThread::sleep_forit compiled but crashed after more than 2 minutes with this message:
++ MbedOS Error Info ++
Error Status: 0x80010133 Code: 307 Module: 1
Error Message: Mutex: 0x20000CB8, Unknown
Location: 0x80034E9
Error Value: 0x20000CB8
Current Thread: application_unnamed_thread Id: 0x20000D50 Entry: 0x8005AB9 StackSize: 0x1000 StackMem: 0x20006908 SP: 0x2001FF34
For more info, visit: https://mbed.com/s/error?error=0x80010133&tgt=STM32F407VE
-- MbedOS Error Info --
However, after replacing:
Lock.trylock_until(1);
printf("\n Inside the door %s with state %d \n", name, state);
ThisThread::sleep_for(2); //some how process stuck in wait state
with
if (Lock.trylock_until(1)) {
printf("\n Inside the door %s with state %d \n", name, state);
ThisThread::sleep_for(2); //some how process stuck in wait state
Lock.unlock();
}