Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- monitor Smokers {
- enum { PAPER, TOBACCO, MATCHES } materials[3];
- enum { SMOKING, WAITING } state[3];
- condition self[3];
- void smoke(int i) {
- state[i] = WAITING;
- test(i);
- if(state[i] != SMOKING) self[i].wait();
- }
- void stopSmoking(int i) {
- state[i] = WAITING;
- test((i + 2) % 3);
- test((i + 1) % 3);
- }
- void test(int i) {
- //not smoking & has different materials from those on the table
- if(state[i] == WAITING &&
- materials[i] != materials[(i + 2) % 3] &&
- materials[i] != materials[(i + 1) % 3])
- {
- state[i] = SMOKING;
- self[i].signal();
- }
- }
- initialization_code() {
- for(int i = 0; i < 3; i++) {
- state[i] = WAITING;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement