Jaydeep Shah
/
21_MUTEX_LOCK_UNLOCK
MUTEX and its use in MBED MUTEX LOCK and Unlock mechanism
main.cpp
- Committer:
- radhey04ec
- Date:
- 2020-07-25
- Revision:
- 0:b908013d70cd
- Child:
- 1:605f5624661e
File content as of revision 0:b908013d70cd:
/* 21_ MUTEX a Guard at door - Basic program (All thread with same priority level) MUTEX allow only one Thread / Function call inside CS, Mutex have ownership type locking mechanism. Procees /or Thread who locked block,only same process /or thread can unlock it. */ //BUGS in MUTEX CLASS //NOTE : MUTEX ONLY PROTECT CS from processes which are belonging same priority level -MBED BUG //PROGRAM CREATED BY : JAYDEEP SHAH -- radheec@gmail.com //DATE : 25 JULY 20 ,VERSION 1.0 //OUTPUT : USE SERIAL TERMINAL 9600 8-N-1 #include "mbed.h" //MBED LIBRARY Mutex M_LOCK; // Create MUTEX OBJECT -class = MUTEX //Create Two Thread Thread t2; //Create Thread with high priority Thread t3; //Create Thread with low priority //Here below section act as CS critical section void common_function(const char *name, int state) //TWO ARGUMENTS 1) WHO CALLED THIS FUNCTION 2)STATE OF CALLER { 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 --------- } //Function -- We will connect more than one thread with this function //So because of context switching this Function become sharable between multiple process. 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 }