István Cserny / Mbed 2 deprecated Lab08_dpp_easy

Dependencies:   mbed mbed-rtos

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 
00004 Semaphore s(5);                        // a pool of 5 chopsticks
00005 Timer mytime;
00006 
00007 void notify(const char* name) {
00008     printf("%s acquired two chopsticks %8.1f\n\r", name,mytime.read());
00009 }
00010 
00011 void test_thread(void const* args) {
00012     while (true) {
00013         Thread::wait(1000+rand()%500); //Thinking   
00014         s.wait();
00015         s.wait();        
00016         notify((const char*)args);
00017         Thread::wait(500+rand()%500);  //Eating
00018         s.release();
00019         s.release();        
00020     }
00021 }
00022 
00023 int main (void) {
00024     mytime.start();
00025     Thread t2(test_thread, (void *)"Philosopher 2");
00026     Thread t3(test_thread, (void *)"Philosopher 3");
00027     Thread t4(test_thread, (void *)"Philosopher 4");
00028     Thread t5(test_thread, (void *)"Philosopher 5");    
00029     test_thread((void *)"Philosopher 1");
00030 }