-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
concurrent_coroutines3.flx
49 lines (40 loc) · 1.03 KB
/
concurrent_coroutines3.flx
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
fun ack(x:int,y:int):int =>
if x == 0 then y + 1
elif y == 0 then ack(x - 1, 1)
else ack(x - 1, ack(x, y - 1))
endif
;
fun thrid: 1 -> address ="pthread_self()"
requires Posix_headers::pthread_h;
type atom = "::std::atomic_size_t" requires Cxx11_headers::atomic;
proc reset: &atom = "$1->store(0);";
proc incr: &atom * int = "*$1+=$2;";
fun get : atom -> size = "$1.load()";
var kk : atom;
reset &kk;
proc p (i:int) () {
var x = ack(3,10);
assert x > 0;
incr (&kk,1);
}
var q = p;
proc many(threading:bool)
{
reset &kk;
if threading do
spawn_process { ; };
spawn_process { ; };
spawn_process { ; };
spawn_process { ; };
spawn_process { ; };
spawn_process { ; };
done
for (var i=0; i<200; ++i;) perform spawn_fthread (q i);
}
println$ "Spawn after launch";
var start = time();
async_run { many(false); };
println$ "Done Serial kk = " + kk.get.str + ", elapsed=" + (time() - start).str;
start = time();
async_run { many(true); };
println$ "Done Concurrent kk = " + kk.get.str + ", elapsed=" + (time() - start).str;