Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- 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