-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparallel.js
More file actions
50 lines (43 loc) · 2.98 KB
/
Copy pathparallel.js
File metadata and controls
50 lines (43 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import * as C from 'cigar';
import { run, printf } from './common';
/**
* This demo shows you can create "fiber" -- each has it's own context and runs in parallel.
*
*/
let task1 = SEQ(
LET("i", 0), // let i = 0;
WHILE(({i}) => i <= 5 ). // while (i <= 5)
DO( // {
({i}) => printf('from task 1: i = %d', i), // printf('from task 1: i = %d', i);
SLEEP(1000), // sleep(1000);
(local) => { local.i ++ } // i ++;
) // }
);
let task2 = SEQ(
LET("i", 0), // let i = 0;
WHILE(({i}) => i <= 3 ). // while (i <= 3)
DO( // {
({i}) => printf('from task 2: i = %d', i), // printf('from task 2: i = %d', i);
SLEEP(1500), // sleep(1500);
(local) => { local.i ++ } // i ++;
) // }
);
/**
* This will launch task1 and task2 in parallel, and once both tasks are completed, appMain will complete.
*/
let appMain = PARA(task1, task2);
/**
* Output:
* from task 1: i = 0
* from task 2: i = 0
* from task 1: i = 1
* from task 2: i = 1
* from task 1: i = 2
* from task 2: i = 2
* from task 1: i = 3
* from task 1: i = 4
* from task 2: i = 3
* from task 1: i = 5
* Done
*/
run(appMain);