source: CIVL/examples/concurrency/dining.cvl@ 2b5dc93

1.23 2.0 main test-branch
Last change on this file since 2b5dc93 was 793cfc2, checked in by Tim Zirkel <zirkeltk@…>, 13 years ago

Reorganized examples and tests. Added algebra, assoc, dining, assume, and scoping examples.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@163 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 843 bytes
Line 
1#include <civlc.h>
2
3int n = 5;
4// Each chopstick will be on the table (0) or in a hand (1).
5int chopsticks[n];
6
7void 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
25void init() {
26 // Put all chopsticks on the table.
27 for (int i = 0; i < n; i++) {
28 chopsticks[i] = 0;
29 }
30}
31
32void 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.