diff options
| author | nodist <kevin.comas.git@gmail.com> | 2026-06-17 13:39:38 -0400 |
|---|---|---|
| committer | nodist <kevin.comas.git@gmail.com> | 2026-06-17 13:39:38 -0400 |
| commit | 7d8de55c79834731c5346303e29cb39c30f55d35 (patch) | |
| tree | 40e88898b54331088654641ce7976ade2c64e3bb /shared_counter.kpl | |
| parent | a4a253d10098210fe22c6fe6f2aef09c32b6b67c (diff) | |
shared counter example
Diffstat (limited to 'shared_counter.kpl')
| -rw-r--r-- | shared_counter.kpl | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/shared_counter.kpl b/shared_counter.kpl new file mode 100644 index 0000000..0be8c24 --- /dev/null +++ b/shared_counter.kpl @@ -0,0 +1,53 @@ + +// Notice how counter can only be modified as read as [c] + +counter : `shared 0 + +inc_dec :([amount] + header : `const `format "WHITE#Counter:# " + body : ^ counter {[c] + c +: amount + ? { + amount = 1 { "#GREEN#%#" `format c } + amount = -1 { "#RED#%#" `format c } + { "INVALID" } + } + } + `log ,(header; body; "\n") +) + +inc : inc_dec `bind 1 +dec : inc_dec `bind -1 + +actions : `const 1_000 + +batch : `const 100 + +runner : ([fn] + total : actions + @ total { + `log "#BOLD#WHITE#Batch: #CYAN#% #MAGENTA#%#\n" `format (fn; total) + v : Vector[`async_type fn] $ () + @ 1 .. batch { v `push `async fn } + @ v {[x] `await x } + total -: batch + } +) + +i : runner `async inc +d : runner `async dec + +`use "time" [Seconds] +`use "sys" [sleep] + +sleep `sync Seconds $ 1 + +`await (d; i) + +`log ^ counter {[c] + ? !c { + `format "#BOLD#GREEN#VALID#\n" + } { + `format "#BOLD#RED#INVALID#\n" + } +} |
