RTOS example - solving the Dining Philosophers problem by using mutexes
main.cpp@0:bc0e168010f0, 2022-02-24 (annotated)
- Committer:
- cspista
- Date:
- Thu Feb 24 08:09:20 2022 +0000
- Revision:
- 0:bc0e168010f0
final version
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| cspista | 0:bc0e168010f0 | 1 | #include "mbed.h" |
| cspista | 0:bc0e168010f0 | 2 | #include "rtos.h" |
| cspista | 0:bc0e168010f0 | 3 | // Mutex stdio_mutex; |
| cspista | 0:bc0e168010f0 | 4 | Mutex chopstick[5]; //Array of mutexes representing the 5 chopsticks |
| cspista | 0:bc0e168010f0 | 5 | Serial pc(USBTX,USBRX); //UART via ST-Link |
| cspista | 0:bc0e168010f0 | 6 | |
| cspista | 0:bc0e168010f0 | 7 | void notify(int num, int state) |
| cspista | 0:bc0e168010f0 | 8 | { |
| cspista | 0:bc0e168010f0 | 9 | // stdio_mutex.lock(); |
| cspista | 0:bc0e168010f0 | 10 | if(state) { |
| cspista | 0:bc0e168010f0 | 11 | pc.printf("Philospher %d is EATING \n\r", num); |
| cspista | 0:bc0e168010f0 | 12 | } else { |
| cspista | 0:bc0e168010f0 | 13 | pc.printf("Philospher %d is thinking \n\r", num); |
| cspista | 0:bc0e168010f0 | 14 | } |
| cspista | 0:bc0e168010f0 | 15 | // stdio_mutex.unlock(); |
| cspista | 0:bc0e168010f0 | 16 | } |
| cspista | 0:bc0e168010f0 | 17 | |
| cspista | 0:bc0e168010f0 | 18 | void philosopher(void const *args) |
| cspista | 0:bc0e168010f0 | 19 | { |
| cspista | 0:bc0e168010f0 | 20 | while (true) { |
| cspista | 0:bc0e168010f0 | 21 | if(chopstick[(int)args-1].trylock()) { |
| cspista | 0:bc0e168010f0 | 22 | if(chopstick[(int)args%5].trylock()) { |
| cspista | 0:bc0e168010f0 | 23 | notify((int)args,1); //Start EATING |
| cspista | 0:bc0e168010f0 | 24 | Thread::wait(1000+rand()%1000); |
| cspista | 0:bc0e168010f0 | 25 | chopstick[(int)args%5].unlock(); //Release chopsticks |
| cspista | 0:bc0e168010f0 | 26 | chopstick[(int)args-1].unlock(); |
| cspista | 0:bc0e168010f0 | 27 | notify((int)args,0); //Start Thinking |
| cspista | 0:bc0e168010f0 | 28 | Thread::wait(2000+rand()%2000); //Get's hungry after this time... |
| cspista | 0:bc0e168010f0 | 29 | } else { |
| cspista | 0:bc0e168010f0 | 30 | chopstick[(int)args-1].unlock(); |
| cspista | 0:bc0e168010f0 | 31 | Thread::wait(100+rand()%100); //Wait for random time if failed |
| cspista | 0:bc0e168010f0 | 32 | } |
| cspista | 0:bc0e168010f0 | 33 | } else { |
| cspista | 0:bc0e168010f0 | 34 | Thread::wait(100+rand()%100); //Wait for random time if failed |
| cspista | 0:bc0e168010f0 | 35 | } |
| cspista | 0:bc0e168010f0 | 36 | |
| cspista | 0:bc0e168010f0 | 37 | } |
| cspista | 0:bc0e168010f0 | 38 | } |
| cspista | 0:bc0e168010f0 | 39 | |
| cspista | 0:bc0e168010f0 | 40 | int main() |
| cspista | 0:bc0e168010f0 | 41 | { |
| cspista | 0:bc0e168010f0 | 42 | pc.baud(115200); |
| cspista | 0:bc0e168010f0 | 43 | Thread t2(philosopher, (void *)2U); |
| cspista | 0:bc0e168010f0 | 44 | Thread t3(philosopher, (void *)3U); |
| cspista | 0:bc0e168010f0 | 45 | Thread t4(philosopher, (void *)4U); |
| cspista | 0:bc0e168010f0 | 46 | Thread t5(philosopher, (void *)5U); |
| cspista | 0:bc0e168010f0 | 47 | philosopher((void *)1U); |
| cspista | 0:bc0e168010f0 | 48 | } |