1.23
2.0
main
test-branch
| Line | |
|---|
| 1 | #include <civlc.h>
|
|---|
| 2 |
|
|---|
| 3 | int n = 5;
|
|---|
| 4 | // Each chopstick will be on the table (0) or in a hand (1).
|
|---|
| 5 | int chopsticks[n];
|
|---|
| 6 |
|
|---|
| 7 | void dine(int id) {
|
|---|
| 8 | int left = id;
|
|---|
| 9 | int right = (id + 1) % n;
|
|---|
| 10 | while (0==0) {
|
|---|
| 11 | if (id % 2 == 0) {
|
|---|
| 12 | $when (chopsticks[left] == 0) {chopsticks[left] = 1;}
|
|---|
| 13 | $when (chopsticks[right] == 0) {chopsticks[right] = 1;}
|
|---|
| 14 | chopsticks[right] = 0;
|
|---|
| 15 | chopsticks[left] = 0;
|
|---|
| 16 | } else {
|
|---|
| 17 | $when (chopsticks[right] == 0) {chopsticks[right] = 1;}
|
|---|
| 18 | $when (chopsticks[left] == 0) {chopsticks[left] = 1;}
|
|---|
| 19 | chopsticks[right] = 0;
|
|---|
| 20 | chopsticks[left] = 0;
|
|---|
| 21 | }
|
|---|
| 22 | }
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | void init() {
|
|---|
| 26 | // Put all chopsticks on the table.
|
|---|
| 27 | for (int i = 0; i < n; i++) {
|
|---|
| 28 | chopsticks[i] = 0;
|
|---|
| 29 | }
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | void main() {
|
|---|
| 33 | $proc philosophers[n];
|
|---|
| 34 |
|
|---|
| 35 | init();
|
|---|
| 36 | for (int i = 0; i < n; i++) {
|
|---|
| 37 | philosophers[i] = $spawn dine(i);
|
|---|
| 38 | }
|
|---|
| 39 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.