IssueRaiser

next steps for CPS

It's an open secret that I've some criticisms of async/await: ## Fear of Threads From the perspective of a newcomer who is looking to transfer existing knowledge or code from another language to Nim, or someone who has zero experience with concurrency in any language, I recommend threads. Here follows the first example from the `threads.nim` documentation. I tweaked the syntax a touch -- _it was written in 2011_ -- but that is all. ```nim import locks type Payload = tuple[a, b: int] var thr: array[4, Thread[Payload]] L: Lock proc threadFunc(interval: Payload) {.thread.} = for i in interval.a .. interval.b: withLock L: echo i initLock L for i in 0 .. thr.high: createThread(thr[i], threadFunc, (i*10, i*10+5)) joinThreads thr deinitLock L ``` Here's the compilation output: ``` $ nim c --gc:arc --define:danger --threads:on -f th.nim .....CC: stdlib_assertions.nim CC: stdlib_io.nim CC: stdlib_system.nim CC: stdlib_locks.nim CC: th.nim Hint: 22456 lines; 0.121s; 20.68MiB peakmem; Dangerous Release build; proj: /home/adavidoff/th.nim; out: /home/adavidoff/th [SuccessX] ``` Here's the codegen: ``` Language files blank comment code ------------------------------------------------------------------------------- C 5 57 54 3733 ``` The resulting binary is 52,472 bytes in size and links against: ``` -rwxr-xr-x. 1 root root 147232 Nov 2 22:20 /lib64

Posted by disruptek on Dec 3, 2020, 6:05 AM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.3,000 issues0 comments

github (Other)

next steps for CPS

disruptekDec 3, 2020, 6:05 AM

It's an open secret that I've some criticisms of async/await: ## Fear of Threads From the perspective of a newcomer who is looking to transfer existing knowledge or code from another language to Nim, or someone who has zero experience with concurrency in any language, I recommend threads. Here follows the first example from the `threads.nim` documentation. I tweaked the syntax a touch -- _it was written in 2011_ -- but that is all. ```nim import locks type Payload = tuple[a, b: int] var thr: array[4, Thread[Payload]] L: Lock proc threadFunc(interval: Payload) {.thread.} = for i in interval.a .. interval.b: withLock L: echo i initLock L for i in 0 .. thr.high: createThread(thr[i], threadFunc, (i*10, i*10+5)) joinThreads thr deinitLock L ``` Here's the compilation output: ``` $ nim c --gc:arc --define:danger --threads:on -f th.nim .....CC: stdlib_assertions.nim CC: stdlib_io.nim CC: stdlib_system.nim CC: stdlib_locks.nim CC: th.nim Hint: 22456 lines; 0.121s; 20.68MiB peakmem; Dangerous Release build; proj: /home/adavidoff/th.nim; out: /home/adavidoff/th [SuccessX] ``` Here's the codegen: ``` Language files blank comment code ------------------------------------------------------------------------------- C 5 57 54 3733 ``` The resulting binary is 52,472 bytes in size and links against: ``` -rwxr-xr-x. 1 root root 147232 Nov 2 22:20 /lib64

Comments (0)

No comments yet. Start the discussion.

Comment on this issue