| | 54 | Dining philosophers: |
| | 55 | |
| | 56 | {{{ |
| | 57 | $input int B = 4; // upper bound on number of philosophers |
| | 58 | $input int n; // number of philosophers |
| | 59 | $assume(2<=n && n<=B); |
| | 60 | |
| | 61 | _Bool forks[n]; // Each fork will be on the table ($true) or in a hand ($false). |
| | 62 | |
| | 63 | void dine(int id) { |
| | 64 | int left = id; |
| | 65 | int right = (id + 1) % n; |
| | 66 | while (1) { |
| | 67 | $when (forks[left]) forks[left] = $false; |
| | 68 | $when (forks[right]) forks[right] = $false; |
| | 69 | forks[right] = $true; |
| | 70 | forks[left] = $true; |
| | 71 | } |
| | 72 | } |
| | 73 | |
| | 74 | void main() { |
| | 75 | $for(int i: 0..n-1) forks[i] = $true; |
| | 76 | $parfor(int i: 0..n-1) dine(i); |
| | 77 | } |
| | 78 | }}} |
| | 79 | |