István Cserny / Mbed 2 deprecated Lab08_dpp_easy

Dependencies:   mbed mbed-rtos

Revision:
0:3def6e6a9e93
diff -r 000000000000 -r 3def6e6a9e93 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Mar 17 12:34:27 2022 +0000
@@ -0,0 +1,30 @@
+#include "mbed.h"
+#include "rtos.h"
+
+Semaphore s(5);                        // a pool of 5 chopsticks
+Timer mytime;
+
+void notify(const char* name) {
+    printf("%s acquired two chopsticks %8.1f\n\r", name,mytime.read());
+}
+
+void test_thread(void const* args) {
+    while (true) {
+        Thread::wait(1000+rand()%500); //Thinking   
+        s.wait();
+        s.wait();        
+        notify((const char*)args);
+        Thread::wait(500+rand()%500);  //Eating
+        s.release();
+        s.release();        
+    }
+}
+
+int main (void) {
+    mytime.start();
+    Thread t2(test_thread, (void *)"Philosopher 2");
+    Thread t3(test_thread, (void *)"Philosopher 3");
+    Thread t4(test_thread, (void *)"Philosopher 4");
+    Thread t5(test_thread, (void *)"Philosopher 5");    
+    test_thread((void *)"Philosopher 1");
+}
\ No newline at end of file