MUTEX and its use in MBED MUTEX LOCK and Unlock mechanism

Committer:
radhey04ec
Date:
Sun Jul 26 11:00:53 2020 +0000
Revision:
2:0cf7e435eeb8
Parent:
1:605f5624661e
FINAL

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 2:0cf7e435eeb8 34 printf("Thread cross & unlock %s: %d\n\r", name, state); //OUTSIDE CODE BLOCK ---------
radhey04ec 0:b908013d70cd 35 M_LOCK.unlock(); //After completing task unlock the code ------------- UNLOCK THE BLOCK
radhey04ec 0:b908013d70cd 36
radhey04ec 1:605f5624661e 37 //DO NOT WRITE any print statement after unlock() it create missunderstanding ....
radhey04ec 1:605f5624661e 38 /* print statement require more clk cycle,after unlock the MUTEX Flag - schedular quickly schedule next thread before print the statement.*/
radhey04ec 0:b908013d70cd 39 }
radhey04ec 0:b908013d70cd 40
radhey04ec 0:b908013d70cd 41
radhey04ec 0:b908013d70cd 42
radhey04ec 0:b908013d70cd 43 //Function -- We will connect more than one thread with this function
radhey04ec 0:b908013d70cd 44 //So because of context switching this Function become sharable between multiple process.
radhey04ec 0:b908013d70cd 45 void test_thread(void const *args)
radhey04ec 0:b908013d70cd 46 {
radhey04ec 0:b908013d70cd 47 while (true) {
radhey04ec 0:b908013d70cd 48 common_function((const char *)args, 0);
radhey04ec 0:b908013d70cd 49 ThisThread::sleep_for(500);
radhey04ec 0:b908013d70cd 50 common_function((const char *)args, 1);
radhey04ec 0:b908013d70cd 51 ThisThread::sleep_for(500);
radhey04ec 0:b908013d70cd 52 }
radhey04ec 0:b908013d70cd 53 }
radhey04ec 0:b908013d70cd 54
radhey04ec 0:b908013d70cd 55 int main()
radhey04ec 0:b908013d70cd 56 {
radhey04ec 0:b908013d70cd 57 t2.start(callback(test_thread, (void *)"Th 2"));
radhey04ec 0:b908013d70cd 58 t3.start(callback(test_thread, (void *)"Th 3"));
radhey04ec 0:b908013d70cd 59
radhey04ec 0:b908013d70cd 60 test_thread((void *)"Th 1"); // DIRECT CALL via main thread
radhey04ec 0:b908013d70cd 61 }