MUTEX and its use in MBED MUTEX LOCK and Unlock mechanism

Committer:
radhey04ec
Date:
Sun Jul 26 10:59:21 2020 +0000
Revision:
1:605f5624661e
Parent:
0:b908013d70cd
Child:
2:0cf7e435eeb8
UPDATE to clear confusion of priority of thread and mutex. But there is no relation. prinf misguide me

Who changed what in which revision?

UserRevisionLine numberNew contents of line
radhey04ec 1:605f5624661e 1 /* 21_ MUTEX a Guard at door - Basic program
radhey04ec 0:b908013d70cd 2 MUTEX allow only one Thread / Function call inside CS, Mutex have ownership type locking mechanism.
radhey04ec 0:b908013d70cd 3 Procees /or Thread who locked block,only same process /or thread can unlock it.
radhey04ec 0:b908013d70cd 4 */
radhey04ec 1:605f5624661e 5
radhey04ec 0:b908013d70cd 6
radhey04ec 0:b908013d70cd 7 //PROGRAM CREATED BY : JAYDEEP SHAH -- radheec@gmail.com
radhey04ec 0:b908013d70cd 8 //DATE : 25 JULY 20 ,VERSION 1.0
radhey04ec 0:b908013d70cd 9
radhey04ec 0:b908013d70cd 10 //OUTPUT : USE SERIAL TERMINAL 9600 8-N-1
radhey04ec 0:b908013d70cd 11
radhey04ec 0:b908013d70cd 12
radhey04ec 0:b908013d70cd 13 #include "mbed.h" //MBED LIBRARY
radhey04ec 0:b908013d70cd 14
radhey04ec 0:b908013d70cd 15 Mutex M_LOCK; // Create MUTEX OBJECT -class = MUTEX
radhey04ec 0:b908013d70cd 16
radhey04ec 0:b908013d70cd 17 //Create Two Thread
radhey04ec 0:b908013d70cd 18
radhey04ec 1:605f5624661e 19 Thread t2;
radhey04ec 0:b908013d70cd 20
radhey04ec 1:605f5624661e 21 Thread t3;
radhey04ec 0:b908013d70cd 22
radhey04ec 0:b908013d70cd 23
radhey04ec 0:b908013d70cd 24 //Here below section act as CS critical section
radhey04ec 0:b908013d70cd 25
radhey04ec 0:b908013d70cd 26 void common_function(const char *name, int state) //TWO ARGUMENTS 1) WHO CALLED THIS FUNCTION 2)STATE OF CALLER
radhey04ec 0:b908013d70cd 27 {
radhey04ec 0:b908013d70cd 28 printf("Thread arrive at door %s: %d\n\r", name, state);
radhey04ec 0:b908013d70cd 29
radhey04ec 0:b908013d70cd 30 M_LOCK.lock(); //After arrive lock the code---------------------------LOCK THE BLOCK
radhey04ec 0:b908013d70cd 31
radhey04ec 0:b908013d70cd 32 printf("This Thread lock the code %s: %d\n\r", name, state);
radhey04ec 0:b908013d70cd 33 wait(0.5); //sleep
radhey04ec 1:605f5624661e 34 printf("Thread cross & unlock %s: %d\n\r", name, state); //OUTSIDE CODE BLOCK ---------
radhey04ec 1:605f5624661e 35
radhey04ec 0:b908013d70cd 36 M_LOCK.unlock(); //After completing task unlock the code ------------- UNLOCK THE BLOCK
radhey04ec 0:b908013d70cd 37
radhey04ec 1:605f5624661e 38 //DO NOT WRITE any print statement after unlock() it create missunderstanding ....
radhey04ec 1:605f5624661e 39 /* print statement require more clk cycle,after unlock the MUTEX Flag - schedular quickly schedule next thread before print the statement.*/
radhey04ec 0:b908013d70cd 40 }
radhey04ec 0:b908013d70cd 41
radhey04ec 0:b908013d70cd 42
radhey04ec 0:b908013d70cd 43
radhey04ec 0:b908013d70cd 44 //Function -- We will connect more than one thread with this function
radhey04ec 0:b908013d70cd 45 //So because of context switching this Function become sharable between multiple process.
radhey04ec 0:b908013d70cd 46 void test_thread(void const *args)
radhey04ec 0:b908013d70cd 47 {
radhey04ec 0:b908013d70cd 48 while (true) {
radhey04ec 0:b908013d70cd 49 common_function((const char *)args, 0);
radhey04ec 0:b908013d70cd 50 ThisThread::sleep_for(500);
radhey04ec 0:b908013d70cd 51 common_function((const char *)args, 1);
radhey04ec 0:b908013d70cd 52 ThisThread::sleep_for(500);
radhey04ec 0:b908013d70cd 53 }
radhey04ec 0:b908013d70cd 54 }
radhey04ec 0:b908013d70cd 55
radhey04ec 0:b908013d70cd 56 int main()
radhey04ec 0:b908013d70cd 57 {
radhey04ec 0:b908013d70cd 58 t2.start(callback(test_thread, (void *)"Th 2"));
radhey04ec 0:b908013d70cd 59 t3.start(callback(test_thread, (void *)"Th 3"));
radhey04ec 0:b908013d70cd 60
radhey04ec 0:b908013d70cd 61 test_thread((void *)"Th 1"); // DIRECT CALL via main thread
radhey04ec 0:b908013d70cd 62 }