HW
Dependencies: TextLCD mbed-rtos mbed
Fork of rtos_semaphore by
main.cpp
- Committer:
- timtianyang
- Date:
- 2015-09-22
- Revision:
- 5:8d6be7bf7f47
- Parent:
- 4:88b20795ea60
File content as of revision 5:8d6be7bf7f47:
// Hello World! for the TextLCD #include "mbed.h" #include "Display.h" #include "rtos.h" #define N 5 #define LEFT (i+N-1)%N #define RIGHT (i+1)%N #define THINKING 0 #define HUNGRY 0 #define EATING 2 int state[N]; Semaphore mutex(1); Semaphore* s[N]; void take_forks(int i); void philosopher_thread(void const* arg); void put_forks(int i); void test(int i); void eat(){ srand(time(NULL)); int r = rand()%3+2; Thread::wait(r*1000); } void think(){ srand(time(NULL)); int r = rand()%3+3; Thread::wait(r*1000); } void philosopher_thread(void const* arg){ int i=*((int *)arg); while(1){ think(); take_forks(i); eat(); put_forks(i); } } void take_forks(int i){ } void put_forks(int i){ } void test(int i){ } int main() { Display lcd; for(int i=0;i<N;i++){ s[i]= new Semaphore(0); } int a=1; int b=2; int c=3; int d=4; int e=5; Thread t1(philosopher_thread, (void *)&a); Thread t2(philosopher_thread, (void *)&b); Thread t3(philosopher_thread, (void *)&c); Thread t4(philosopher_thread, (void *)&d); Thread t5(philosopher_thread, (void *)&e); int i=0; while(1){ lcd.LCDEat(i); if(i>0){ lcd.LCDDoneEating(i-1); } else{lcd.LCDDoneEating(4);} i++; if(i==5){i=0;} wait(1); } }